Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
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
190
Redis Serach Introduction
huacnlee
13
790
Other Decks in Programming
See All in Programming
Heart of Swift Concurrency
koher
0
910
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.1k
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
210
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
4
2.7k
新卒PdEのリアル
ryu1013
1
500
[GoCon2026] When Goroutines Are Not Enough: Runtime Locality in High-Throughput Go
takehaya
6
2.3k
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
420
What We Talk About When We Talk About XP
m_seki
2
660
大喜利で理解するLLM as a Judge / Understanding LLM-as-a-Judge through Ogiri
rockname
0
150
Java 27新機能 / Java 27 new features
kishida
2
150
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
460
市販E-Readerを乗っ取れ 〜Embedded Swiftで電子ペーパーガジェットを制御する〜
trickart
0
190
Featured
See All Featured
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
540
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
410
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
First, design no harm
axbom
PRO
2
1.3k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
GitHub's CSS Performance
jonrohan
1033
470k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
590
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2.9k
A designer walks into a library…
pauljervisheath
211
25k
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⽇日星期五