【发布时间】:2015-01-13 22:04:39
【问题描述】:
我正在尝试检索我的公开个人资料的墙贴。我需要检查一条消息是否到达我的墙上并在给定的时间戳内传递。我实际上是在编写一个监控检查来验证我们的消息传递系统的消息传递。因为目标机器主动拒绝它,我得到了一个 No Connection 可以建立。不太清楚为什么?
#!/usr/bin/env python
# Many times when crawling we run into problems where content that is rendered on the page is generated with Javascript and therefore scrapy is unable to crawl for it (eg. ajax requests, jQuery craziness). However, if you use Scrapy along with the web testing framework Selenium then we are able to crawl anything displayed in a normal web browser.
#
# Some things to note:
# You must have the Python version of Selenium RC installed for this to work, and you must have set up Selenium properly. Also this is just a template crawler. You could get much crazier and more advanced with things but I just wanted to show the basic idea. As the code stands now you will be doing two requests for any given url. One request is made by Scrapy and the other is made by Selenium. I am sure there are ways around this so that you could possibly just make Selenium do the one and only request but I did not bother to implement that and by doing two requests you get to crawl the page with Scrapy too.
#
# This is quite powerful because now you have the entire rendered DOM available for you to crawl and you can still use all the nice crawling features in Scrapy. This will make for slower crawling of course but depending on how much you need the rendered DOM it might be worth the wait.
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
import time
from selenium import selenium
class SeleniumSpider(CrawlSpider):
name = "SeleniumSpider"
start_urls = ["https://www.facebook.com/chronotrackmsgcheck"]
rules = (
Rule(SgmlLinkExtractor(allow=('\.html', )), callback='parse_page',follow=True),
)
def __init__(self):
CrawlSpider.__init__(self)
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*chrome", "https://www.facebook.com/chronotrackmsgcheck")
self.selenium.start()
def __del__(self):
self.selenium.stop()
print self.verificationErrors
CrawlSpider.__del__(self)
def parse_page(self, response):
item = Item()
hxs = HtmlXPathSelector(response)
#Do some XPath selection with Scrapy
hxs.select('//div').extract()
sel = self.selenium
sel.open(response.url)
#Wait for javscript to load in Selenium
time.sleep(2.5)
#Do some crawling of javascript created content with Selenium
sel.get_text("//div")
yield item
SeleniumSpider()
【问题讨论】:
-
如果您没有获得他们的书面许可,他们不允许抓取 Facebook。我建议阅读facebook.com/apps/site_scraping_tos_terms.php
-
@WizKid - “爬行” Facebook 是合法的。 Google 和其他搜索引擎每天都会抓取公开的个人资料。
-
@Grant-Zukel 如果您将用户代理设置为看起来像普通网络爬虫的东西,则无需运行或等待任何 JS。事实上,在这种情况下,您也许可以完全省略 Selenium,而只需获取 HTML 并解析它。我不知道正确的用户代理可能是什么,但从 Google bot 使用的内容开始。 WRT 错误,我无能为力。
-
@sstur 你读过我发布的链接吗?我敢打赌,谷歌已经获得 Facebook 的书面许可。根据facebook.com/robots.txt谷歌机器人和其他一些机器人被允许抓取Facebook。但它以: User-agent: *, Disallow: / 结尾。这意味着对其他人来说是不允许的
-
sstur 你错了,我会尽快发布工作代码。
标签: javascript python facebook selenium scrapy