惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
T
Threatpost
P
Palo Alto Networks Blog
NISL@THU
NISL@THU
O
OpenAI News
Project Zero
Project Zero
G
GRAHAM CLULEY
P
Privacy International News Feed
A
Arctic Wolf
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
D
Docker
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
W
WeLiveSecurity
P
Proofpoint News Feed
腾讯CDC
Cloudbric
Cloudbric
S
Secure Thoughts
C
Check Point Blog
博客园 - Franky
T
The Exploit Database - CXSecurity.com
T
Troy Hunt's Blog
GbyAI
GbyAI
Security Archives - TechRepublic
Security Archives - TechRepublic
Application and Cybersecurity Blog
Application and Cybersecurity Blog
月光博客
月光博客
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
TaoSecurity Blog
TaoSecurity Blog
L
Lohrmann on Cybersecurity
V
Visual Studio Blog
F
Fortinet All Blogs
博客园 - 叶小钗
C
CXSECURITY Database RSS Feed - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
C
Cisco Blogs
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 有安科技

好消息,OpenResty Manager专业版来了,如何免费升级? NanoPi R3S PVE 8.X 优化 鹊桥-零信任安全网格网络 森罗-攻击面管理平台 天机-免费的新一代办公安全平台 南墙WAF-最好的免费Web应用防火墙 Elastic Stack 7.4.0~7.5.0 白金版永不过期 JVM CPU占满问题定位 SQL2008数据库优化常用脚本 Tips of Linux C programming nginx url解码引发的waf漏洞 http长连接200万尝试及调优 发个在owasp上演讲web应用防火墙的ppt linux程序调试 Using Internet Explorer from .NET 快速构建实时抓取集群 Safe3 Web漏洞扫描系统 v9.6免费版 Varnish+Xcache构建高性能WEB构架初探 IIS监控请求脚本
scrapy结合webkit抓取js生成的页面
有安科技 · 2011-10-19 · via 博客园 - 有安科技

1 scedule

scrapy 作为抓取框架,包括了spider,pipeline基础设施

2 webkit

scrapy 本身不能作为js engine,这就导致很多js生成的页面的数据会无法抓取到,因此,一些通用做法是webkit或者xmi_runner(firefox)。通过这个手段可以对于js生成的数据进行抓取。需要安装的包有

python-webkit (相关依赖自行解决)

Xvfb (用于非Xwindow环境)

3 开发downloader middleware

from scrapy.http import Request, FormRequest, HtmlResponse
 
import gtk
import webkit
import jswebkit
import settings
 
class WebkitDownloader( object ):
    def process_request( self, request, spider ):
        if spider.name in settings.WEBKIT_DOWNLOADER:
            if( type(request) is not FormRequest ):
                webview = webkit.WebView()
                webview.connect( 'load-finished', lambda v,f: gtk.main_quit() )
                webview.load_uri( request.url )
                gtk.main()
                js = jswebkit.JSContext( webview.get_main_frame().get_global_context() )
                renderedBody = str( js.EvaluateScript( 'document.body.innerHTML' ) )
                return HtmlResponse( request.url, body=renderedBody )

4 配置

在scrapy的settings.py中加入:

#which spider should use WEBKIT
WEBKIT_DOWNLOADER=['ccb']
 
DOWNLOADER_MIDDLEWARES = {
    'rate_crawler.dowloader.WebkitDownloader': 543,
}   
 
import os
os.environ["DISPLAY"] = ":0"

5 使用

启动 Xvfb (假设DISPLAY=:0)

要与settings.py中的DISPLAY对应(本例中是:0)。

scrapy crawl xxx