【问题标题】:Scraping some Facebook data but not all? Scrapy/Splash/Python抓取一些 Facebook 数据但不是全部? Scrapy/Splash/Python
【发布时间】:2018-12-31 02:59:25
【问题描述】:

我有一个看起来像这样的蜘蛛:

import scrapy
from scrapy_splash import SplashRequest

class BarkbotSpider(scrapy.Spider):
    name = 'barkbot'
    start_urls = [
        'http://www.facebook.com/pg/TheBarkFL/events/?ref=page_internal/'
    ]
    custom_settings = {
        'FEED_URI': 'output/barkoutput.json'
    }

    def start_requests(self):
        for url in self.start_urls:
            yield SplashRequest(
                url,
                self.parse,
            )

    def parse(self, response):
        for href in response.css("div#upcoming_events_card a::attr(href)").extract():
            yield response.follow(href, self.parse_concert)

    def parse_concert(self, response):
        concert = {

            "headliner" : response.xpath(
                "//h1[@id='seo_h1_tag']/text()"
            ).extract_first(),

            "venue" : "The Bark",
            "venue_address" : "507 All Saints St.",
            "venue_website" : "https://www.facebook.com/TheBarkFL",

            "date_time" : response.xpath(
                "//li[@id='event_time_info']//text()"
            ).extract(),

            "notes" : response.xpath(
                "//div[@data-testid='event-permalink-details']/span/text()"
            ).extract()

        }

        if concert['headliner']:
            yield concert

我运行蜘蛛并成功完成。但是所有“notes”和“date_time”键返回的是空列表。我对注释一感到特别困惑,因为这似乎相当简单,除非 xpath 不能使用 data-testid 作为属性。但是,我已成功抓取了标题键,因此显然我正在连接到每个页面。

我不熟悉抓取 JavaScript 生成的内容和 Splash,但我设法让另一只蜘蛛成功工作,只是在 Facebook 上没有。什么给了?

【问题讨论】:

  • Splash 是否允许您访问浏览器发出的网络请求?因为将事件数据从他们对 graphql 端点的 XHR 调用中提取出来会很棒,因为它是结构化数据
  • Facebook 上不允许抓取,所以不要那样做。你必须使用 api。

标签: python facebook scrapy splash-screen


【解决方案1】:

除非 xpath 不能使用 data-testid 作为属性

不,不是这样;我刚刚检查了 Scrapy 1.5.1,您的 xpath 与示例文档匹配得很好。它甚至与该文档中的其他 data-testid 属性匹配,所以我很确定您遇到了竞争条件,因为 event-permalink-details 没有出现在 HTML 中;它是从 XHR 调用加载到他们的 graphql 端点的。由于 Splash,在您的情况下这可能没问题,但如果您的选择器不匹配,则该选择器在 XHR 解决之前运行。我对 Splash 了解的不够多,无法帮助解决这种情况。


我不知道您的date_time 问题的答案,但实际上我敢打赌您真正想要的是.xpath('//li[@id="event_time_info"]//@content'),因为它包含2019-01-03T17:30:00-08:00 to 2019-01-03T20:30:00-08:00,这似乎比不合格的text() 匹配的字符串块要好得多

【讨论】:

    猜你喜欢
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 2020-11-27
    • 2021-12-01
    • 2013-05-27
    • 2020-05-04
    相关资源
    最近更新 更多