一种方法是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页面的更多相关文章

  1. Winfrom 抓取web页面内容代码

    WebRequest request = WebRequest.Create("http://1.bjapp.sinaapp.com/play.php?a=" + PageUrl) ...

  2. 简易数据分析 13 | Web Scraper 抓取二级页面

    这是简易数据分析系列的第 13 篇文章. 不知不觉,web scraper 系列教程我已经写了 10 篇了,这 10 篇内容,基本上覆盖了 Web Scraper 大部分功能.今天的内容算这个系列的最 ...

  3. Python使用lxml模块和Requests模块抓取HTML页面的教程

    Web抓取Web站点使用HTML描述,这意味着每个web页面是一个结构化的文档.有时从中 获取数据同时保持它的结构是有用的.web站点不总是以容易处理的格式, 如 csv 或者 json 提供它们的数 ...

  4. Python和Ruby抓取网页时的中文乱码问题(在Eclipse和Apatana Studio下均是这种解决方法

    Python抓取中文网页乱码 :Eclipse+pydev2.2+python2.7  :Apatana Studio3+ pydev2.2+python2.7      run时设置 run--&g ...

  5. 利用curl抓取远程页面内容

    最基本的操作如下 $curlPost = 'a=1&b=2';//模拟POST数据$cookie_file    =    tempnam('./temp','kie');//可选,保存ses ...

  6. php curl抓取远程页面内容的代码

    使用php curl抓取远程页面内容的例子. 代码如下: <?php /** * php curl抓取远程网页内容 * edit by www.jbxue.com */ $curlPost = ...

  7. php抓取ajax页面返回图片。

    要抓取的页面:http://pic.hao123.com/ 当我们往下滚动的时候,图片是用ajax来动态获取的.这就需要我们仔细分析页面了. 可以看到,异步加载的ajax文件为: http://pic ...

  8. C#抓取AJAX页面的内容

    原文 C#抓取AJAX页面的内容 现在的网页有相当一部分是采用了AJAX技术,所谓的AJAX技术简单一点讲就是事件驱动吧(当然这种说法可能很不全面),在你提交了URL后,服务器发给你的并不是所有是页面 ...

  9. c#抓取动态页面WebBrowser

    在ajax横行的年代,很多网页的内容都是动态加载的,而我们的小爬虫抓取的仅仅是web服务器返回给我们的html,这其中就 跳过了js加载的部分,也就是说爬虫抓取的网页是残缺的,不完整的,下面可以看下博 ...

随机推荐

  1. 【Unity Shaders】ShadowGun系列之二——雾和体积光

    写在前面 体积光,这个名称是God Rays的中文翻译,感觉不是很形象.God Rays其实是Crepuscular rays在图形学中的说法,而Crepuscular rays的意思是云隙光.曙光. ...

  2. ServletContainerInitializer初始化器

    在web容器启动时为提供给第三方组件机会做一些初始化的工作,例如注册servlet或者filtes等,servlet规范中通过ServletContainerInitializer实现此功能.每个框架 ...

  3. 01基于配置文件方式的SpringMVC,三种HandlerMapping,三种控制器

     1 添加Spring MVC所需的jar包. 2 创建一个以下项目结构的springmvc项目 3 web.xml的配置如下: <?xmlversion="1.0"en ...

  4. 后端分布式系列:分布式存储-HDFS 异常处理与恢复

    在前面的文章 <HDFS DataNode 设计实现解析>中我们对文件操作进行了描述,但并未展开讲述其中涉及的异常错误处理与恢复机制.本文将深入探讨 HDFS 文件操作涉及的错误处理与恢复 ...

  5. 应用UUID简化设计

    应用UUID简化设计(金庆的专栏)UUID(Universally Unique Identifier) 保证每次生成的都是唯一的,不同机器生成UUID也能保证唯一.网游中使用UUID可以避免全局的I ...

  6. 记录github出错及解决方案

    刚刚在github上更新自己项目的一些内容时出现了一些错误,几经折腾及在网上查找资料终于解决.具体记录如下: 主要就是就是在push时报错,错误信息如下: 根据报错信息原本以为是要重新pull一下,但 ...

  7. UNIX网络编程——套接字选项(SOL_SOCKET级别)

    #include <sys/socket.h> int setsockopt( int socket, int level, int option_name,const void *opt ...

  8. 关于APP界面布局设计的八种优缺点

    学习UI设计的时候,经常要接触到页面的布局,布局的方式会直接影响一个APP的视觉效果,好的布局方式,往往能带来舒服的视觉效果,更能得到用户的接受与好评.然而万变不离其宗,移动端页面常用的布局,不外乎以 ...

  9. Java:将字符串中的数字转换成整型

    在C语言中,将字符串中的数字转换为整型的方法是是利用atoi这个函数.在Java中,我们可以利用parseInt方法来实现,具体代码如下: public class HelloWorld { publ ...

  10. Java进阶(二十八)SimpleDateFormat格式化日期问题

    SimpleDateFormat格式化日期问题 前言 发现一个问题,经过以下语句处理后,发现12:27:45转换后成为了00:27:45. DateFormat df = null; if(DATE1 ...