ruby抓取web页面
一种方法是Net::HTTP.new方法,返回resp码和实际的data:
require 'net/http'
h = Net::HTTP.new("www.baidu.com",80)
resp,data = h.get("/")
puts resp
puts data
不过resp可以取到,但data返回nil值,换其他网页同样如此.后来发现那是早期的方法返回值,新的ruby只返回一个值,我们可以用resp.body来访问网页内容,坑爹啊:
h = Net::HTTP.new("www.baidu.com",80)
resp = h.get "/"
puts resp.body
还可以用以下方法效果类似:
require 'uri'
resp = Net::HTTP.get_response(URI("http://www.baidu.com/"))
puts resp.body
注意用URI生成的url字符串要以http://开头,否则貌似有错.不过实际中我们要加错误处理和超时处理,否则你就且等吧:
#!/usr/bin/ruby
require 'uri'
require 'timeout'
require 'net/http'
$resp = $data = nil
begin
timeout(5) {
h = Net::HTTP.new(ARGV[0],80)
$resp = h.get("/")
#$resp = Net::HTTP.get_response(URI("http://"+ARGV[0]+"/"))
}
rescue => e
puts e.inspect
exit
end
puts $resp.body
运行结果如下:
wisy@wisy-ThinkPad-X61:~/src/ruby_src$ ./x.rb www.baidu.com|head -c 2000
<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta content="always" name="referrer"><link rel="dns-prefetch" href="//s1.bdstatic.com"/><link rel="dns-prefetch" href="//t1.baidu.com"/><link rel="dns-prefetch" href="//t2.baidu.com"/><link rel="dns-prefetch" href="//t3.baidu.com"/><link rel="dns-prefetch" href="//t10.baidu.com"/><link rel="dns-prefetch" href="//t11.baidu.com"/><link rel="dns-prefetch" href="//t12.baidu.com"/><link rel="dns-prefetch" href="//b1.bdstatic.com"/><title>百度一下,你就知道</title>
<style index="index" id="css_index">html,body{height:100%}html{overflow-y:auto}#wrapper{position:relative;_position:;min-height:100%}#head{padding-bottom:100px;text-align:center;*z-index:1}#ftCon{height:100px;position:absolute;bottom:44px;text-align:center;width:100%;margin:0 auto;z-index:0;overflow:hidden}#ftConw{width:720px;margin:0 auto}body{font:12px arial;text-align:;background:#fff}body,p,form,ul,li{margin:0;padding:0;list-style:none}body,form,#fm{position:relative}td{text-align:left}img{border:0}a{color:#00c}a:active{color:#f60}.bg{background-image:url(http://s1.bdstatic.com/r/www/cache/static/global/img/icons_3bfb8e45.png);background-repeat:no-repeat;_background-image:url(http://s1.bdstatic.com/r/www/cache/static/global/img/icons_f72fb1cc.gif)}.bg_tuiguang_browser{width:16px;height:16px;background-position:-600px 0;display:inline-block;vertical-align:text-bottom;font-style:normal;overflow:hidden;margin-right:5px}.bg_tuiguang_browser_big{width:56px;height:56px;position:absolute;left:10px;top:10px;background-position:-600px -24px}
.bg_tuiguang_weishi{width:56px;height:56px;position:absolute;left:10px;top:10px;background-position:-672px -24px}.c-icon{display:inline-block;width:14px;height:14px;vertical-align:text-bottom;font-style normal;overflow:hidden;background:url(http://s1.bdstatic.com/r/www/cache/static/global/img/icons_3bfb8e45../x.rb:19:in `write': Broken pipe @ io_write - <STDOUT> (Errno::EPIPE)
from ./x.rb:19:in `puts'
from ./x.rb:19:in `puts'
from ./x.rb:19:in `<main>'
ruby抓取web页面的更多相关文章
- Winfrom 抓取web页面内容代码
WebRequest request = WebRequest.Create("http://1.bjapp.sinaapp.com/play.php?a=" + PageUrl) ...
- 简易数据分析 13 | Web Scraper 抓取二级页面
这是简易数据分析系列的第 13 篇文章. 不知不觉,web scraper 系列教程我已经写了 10 篇了,这 10 篇内容,基本上覆盖了 Web Scraper 大部分功能.今天的内容算这个系列的最 ...
- Python使用lxml模块和Requests模块抓取HTML页面的教程
Web抓取Web站点使用HTML描述,这意味着每个web页面是一个结构化的文档.有时从中 获取数据同时保持它的结构是有用的.web站点不总是以容易处理的格式, 如 csv 或者 json 提供它们的数 ...
- Python和Ruby抓取网页时的中文乱码问题(在Eclipse和Apatana Studio下均是这种解决方法
Python抓取中文网页乱码 :Eclipse+pydev2.2+python2.7 :Apatana Studio3+ pydev2.2+python2.7 run时设置 run--&g ...
- 利用curl抓取远程页面内容
最基本的操作如下 $curlPost = 'a=1&b=2';//模拟POST数据$cookie_file = tempnam('./temp','kie');//可选,保存ses ...
- php curl抓取远程页面内容的代码
使用php curl抓取远程页面内容的例子. 代码如下: <?php /** * php curl抓取远程网页内容 * edit by www.jbxue.com */ $curlPost = ...
- php抓取ajax页面返回图片。
要抓取的页面:http://pic.hao123.com/ 当我们往下滚动的时候,图片是用ajax来动态获取的.这就需要我们仔细分析页面了. 可以看到,异步加载的ajax文件为: http://pic ...
- C#抓取AJAX页面的内容
原文 C#抓取AJAX页面的内容 现在的网页有相当一部分是采用了AJAX技术,所谓的AJAX技术简单一点讲就是事件驱动吧(当然这种说法可能很不全面),在你提交了URL后,服务器发给你的并不是所有是页面 ...
- c#抓取动态页面WebBrowser
在ajax横行的年代,很多网页的内容都是动态加载的,而我们的小爬虫抓取的仅仅是web服务器返回给我们的html,这其中就 跳过了js加载的部分,也就是说爬虫抓取的网页是残缺的,不完整的,下面可以看下博 ...
随机推荐
- 剑指offer面试题6 重建二叉树(java)
注:(1)java中树的构建 (2)构建子树时可以直接利用Arrays.copyOfRange(preorder, from, to),这个方法是左开右闭的 package com.xsf.SordF ...
- Zookeeper总概
zookeeper是一个开源的分布式协调服务.是典型的分布式数据一致性的解决方案. zookeeper可以保证以下分布式一致性的特性 1. 顺序性:同一客户端发起的事务请求,最终会严格的按照发出顺序应 ...
- linux crontab定时任务详解
1. 为当前用户创建cron服务: crontab -e 例如 文件内容如下(每隔1分钟执行sql脚本): */1 * * * * mysql -h127.0.0.1 -uroot -proot ...
- 算法之路(二)呈现O(logN)型的三个算法
典型时间复杂度 我们知道算法的执行效率,可以从它的时间复杂度来推算出一二.而典型的时间复杂度有哪些类型呢? 由上图,可以看出,除了常数时间复杂度外,logN型的算法效率是最高的.今天就介绍三种非常ea ...
- 【ShaderToy】开篇
写在前面 呜呼,好久没有写博客了,好惭愧.题外话,感觉越大就越想家,希望可以一直和家人在一起,哪怕只是坐在一起不说话也觉得很温暖,一想到要分开眼睛就开始酸,哎.开学还是爬上来老实更新博客学习吧~ 今天 ...
- ubuntu和mac OS X下另一种使用QQ的方法
在ubuntu可以到pidgin官网下载http://www.pidgin.im,然后再安装插件 pidgin-lwqq即可,步骤为: sudo add-apt-repository ppa:lain ...
- Android摄像头照相机技术-android学习之旅(八)
简介 Android SDK支持Android设备内置的照相机.从Android2.3开始支持多个摄像头(主要指前置摄像头和后置摄像头).通过照片相可以拍照和录像. 需要考虑的问题 是否支持照相机 快 ...
- androidpn-client笔记及BUG修改
这几天应业务需要,在搭建一个推送的DEMO.在参考了许多资料之后,最终使用了androidpn. androidpn分server端和client端.server端几经折腾,最终采用了github上的 ...
- MyBatis主键生成器SelectKeyGenerator(三)
前面两篇博客我们介绍了MyBatis主键生成器KeyGenerator(一)和MyBatis主键生成器Jdbc3KeyGenerator(二),接下来我们介绍SelectKeyGenerator, 如 ...
- velocity 时间显示 时间格式化 时间转化
思想:通过前台展现层(Velocity)中的工具(tools)方法来修改. *************** 具体修改步骤: 1.在toolbox.xml配置文件中添加以下代码: <tool> ...