Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
How to collect content form Web
Search
Jason Lee
July 27, 2012
Programming
290
7
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
How to collect content form Web
Jason Lee
July 27, 2012
More Decks by Jason Lee
See All by Jason Lee
Ruby China 背后的故事
huacnlee
25
3.8k
Ruby 入门第一课
huacnlee
1
180
Redis Serach Introduction
huacnlee
13
780
Other Decks in Programming
See All in Programming
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
210
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
270
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
260
The NotImplementedError Problem in Ruby
koic
1
920
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
200
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
2
680
RTSPクライアントを自作してみた話
simotin13
0
630
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
170
Inside Stream API
skrb
1
770
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
170
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
610
Creating Composable Callables in Contemporary C++
rollbear
0
160
Featured
See All Featured
Writing Fast Ruby
sferik
630
63k
Prompt Engineering for Job Search
mfonobong
0
350
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
330
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
150
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
390
Designing Experiences People Love
moore
143
24k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
250
Marketing to machines
jonoalderson
1
5.5k
Leo the Paperboy
mayatellez
7
1.9k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.8k
Transcript
如何编写数据采集器 Jason Lee 12年7月27⽇日星期五
Nokogiri Python Ruby Regexp 12年7月27⽇日星期五
Hello world require "nokogiri" require "open-uri" doc = Nokogiri::HTML(open("http://www.cnbeta.com"),nil, "gbk")
doc.css(".newslist .topic a").each do |a| puts "#{a.text} - #{a.attr("href")}" end 摩托罗拉移动总部将搬⾄至芝加哥 涉及3000⼈人 - /articles/199038.htm 步步惊⼼心!解密Google千兆宽带如何降低成本 - /articles/199037.htm 不肯服输 苹果成功申请暂缓执⾏行道歉裁决 - /articles/199035.htm [最新]摩托罗拉Android机型在德国被禁售 - /articles/199034.htm Windows Phone 8版IE 10新特性侧漏 - /articles/199033.htm [视频]⽇日本研发新⼀一代商店付费⽅方案BacaryScan - /articles/199032.htm ⾕谷歌搜索结果⻚页⾯面再改版 左侧全留⽩白 - /articles/199031.htm [视频]预留摄像头位置 ⾕谷歌Nexus 7平板拆机视频曝光 - /articles/199029.htm [图]苹果2006年iPhone原型设计曝光 - /articles/199028.htm 你所不了解的中国字库产业链 - /articles/199027.htm ..... 12年7月27⽇日星期五
Problems • 编码 • 需要登陆 • ⻚页⾯面请求频率限制 • 不规则的内容结构 •
Javascript 产⽣生的内容 • 列表到⼦子⻚页内容 • 图⽚片以及其他⼀一些资源⽂文件 • 需要 POST 请求才能访问的⻚页⾯面,⽐比如搜房⺴⽹网 Asp.net 的翻 ⻚页; 12年7月27⽇日星期五
编码问题 Nokogiri::HTML(open("http://www.cnbeta.com"),nil, "gbk") GB2312 Nokogiri::HTML(open("http://www.cnbeta.com")) UTF-8 12年7月27⽇日星期五
需要登陆的⻚页⾯面 12年7月27⽇日星期五
require 'mechanize' agent = Mechanize.new agent.get("http://www.douban.com/accounts/login") form = agent.page.forms.first #
email form.fields[2].value = "
[email protected]
" # password form.fields[3].value = "jiubugaoshuni" form.submit agent.get("http://www.douban.com/accounts/") puts agent.title https://github.com/tenderlove/mechanize 12年7月27⽇日星期五
访问频率限制 • 设定 200ms 延迟每次⻚页⾯面请求; • ⽤用 Memcached 控制全局的⻚页⾯面下载频率; class
GlobalFetcher def self.read(url) return false if !self.allow? self.hint open(url).read end def self.hint Rails.cache.increment("global_fetcher_#{Time.now.strftime("%Y%m%d%H%M")}" , 1) end def self.count Rails.cache.read("global_fetcher_#{Time.now.strftime("%Y%m%d%H%M")}" ).to_i || 0 end def self.allow? self.count < 40 end end 每分钟 40 次请求 12年7月27⽇日星期五
不规则的结构 <div id="info"> <span><span class="pl">导演</span>: 盖瑞·罗斯</span><br/> <span><span class="pl">编剧</span>: 苏珊·科林斯 /
⽐比利·雷 / 盖瑞·罗斯</span><br/> <span><span class="pl">主演</span>: 詹妮弗·劳伦斯 / 乔什·哈切森</span><br/> <span class="pl">IMDb链接:</span> <a href="...">tt1392170</a><br> </div> <div id="info"> <span><span class="pl">导演</span>: 盖瑞·罗斯</span><br/> <span><span class="pl">主演</span>: 詹妮弗·劳伦斯 / 乔什·哈切森</span><br/> <span><span class="pl">语⾔言</span>: 英⽂文 / 法语</span><br/> <span><span class="pl">国家</span>: 美国</span><br/> <span class="pl">IMDb链接:</span> <a href="...">tt1392170</a><br> </div> Sometimes like this: 12年7月27⽇日星期五
AJAX 内容 12年7月27⽇日星期五
⼦子⻚页⾯面 doc = Nokogiri::HTML(open("http://www.cnbeta.com"),nil, "gbk") doc.css(".newslist .topic a").each do |a|
fetch_content(a.attr("href")) end def fetch_content(path) doc = Nokogiri::HTML(open("http://www.cnbeta.com#{path}"),nil, "gbk") ... end 12年7月27⽇日星期五
需要验证码? 这是⼤大难题... 12年7月27⽇日星期五
技巧 • 通过 RSS 或其他 API 关注内容更新; • 下载⻚页⾯面的⽅方法加⼊入 caching
提升下次⽆无意访问到同样⻚页⾯面的 速度,例如: open-uri-cached; • 伪造 http_referer,以破解某些⽐比较弱的图⽚片防盗链; • 设定登对⺴⽹网站级别的访问延迟控制(Memcached),以防⽌止 速度过快被屏蔽 IP; • 通过 CSS Selector + Regexp 采集⼀一些⽆无规则的结构; • 仔细分析,猜测,找出⻚页⾯面/URL 规则,拿到⼀一些隐藏的内 容; 12年7月27⽇日星期五
其他学习资源 • http://railscasts.com/episodes/173-screen-scraping-with- scrapi • http://railscasts.com/episodes/190-screen-scraping-with- nokogiri • http://railscasts.com/episodes/191-mechanize 12年7月27⽇日星期五
End http://huacnlee.com 12年7月27⽇日星期五