如何结合scrapy和htmlunit用javascript爬取url
我正在使用Scrapy来抓取页面,但是,我无法使用javascript处理页面。 人们建议我使用 htmlunit,所以我安装了它,但我根本不知道如何使用它。有人可以给我一个例子(scrapy + htmlunit)吗?非常感谢。
I'm working on Scrapy to crawl pages,however,I can't handle the pages with javascript.
People suggest me to use htmlunit, so I got it installed,but I don't know how to use it at all.Dose anyone can give an example(scrapy + htmlunit) for me? Thanks very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要使用 javascript 处理页面,您可以使用 Webkit 或 Selenium。
以下是来自 snippets.scrapy.org 的一些片段:
使用 gtk/webkit/jswebkit 渲染/交互式 javascript
使用 Scrapy 和 Selenium RC 渲染的 Javascript 爬虫
To handle the pages with javascript you can use Webkit or Selenium.
Here some snippets from snippets.scrapy.org:
Rendered/interactive javascript with gtk/webkit/jswebkit
Rendered Javascript Crawler With Scrapy and Selenium RC
以下是在下载处理程序中间件中使用 selenium 和 phantomjs 无头 Web 驱动程序的工作示例。
我希望能够告诉不同的蜘蛛使用哪个中间件,所以我实现了这个包装器:
settings.py:
为了使包装器工作,所有蜘蛛必须至少具有:
包含一个中间件:
以这种方式实现它的主要优点而不是在蜘蛛的特点是你最终只会发出一个请求。例如,在 reclosedev 的第二个链接的解决方案中:下载处理程序处理请求,然后将响应交给蜘蛛。然后,蜘蛛在其 parse_page 函数中发出一个全新的请求——这是对相同内容的两个请求。
另一个例子: https://github.com/scrapinghub/scrapyjs
干杯!
Here is a working example using selenium and phantomjs headless webdriver in a download handler middleware.
I wanted to ability to tell different spiders which middleware to use so I implemented this wrapper:
settings.py:
for wrapper to work all spiders must have at minimum:
to include a middleware:
The main advantage to implementing it this way rather than in the spider is that you only end up making one request. In the solution at reclosedev's second link for example: The download handler processes the request and then hands off the response to the spider. The spider then makes a brand new request in it's parse_page function -- That's two requests for the same content.
Another example: https://github.com/scrapinghub/scrapyjs
Cheers!