【问题标题】:Can't figure out why my Scrapy script isn't working无法弄清楚为什么我的 Scrapy 脚本不起作用
【发布时间】:2017-10-31 21:10:55
【问题描述】:
import scrapy

class TestSpider(scrapy.Spider):
    name = 'test'
    start_urls = ['https://go.twitch.tv/directory']
def parse(self, response):
    for title in response.css('body'):
        yield {'title': title.css('h3.tw-box-art-card__title::text').extract()}

    for next_page in response.css('a::attr(href)'):
        yield response.follow(next_page, self.parse)

它只是抓取和抓取https://go.twitch.tv/directory,但不输出任何标题。

我是 Python 新手,所以问题可能很明显,但我无法弄清楚。

【问题讨论】:

  • 您为什么希望您的代码显示任何标题?
  • 因为解析函数和我用来运行脚本的命令“scrapy crawl test -o test.csv
  • 您的代码缩进严重。修复它,您可能会发现它很有帮助。
  • @Massaxe parse 是否真的被调用了?
  • @Massaxe ,该网页的内容是动态生成的,因此您需要使用任何浏览器模拟器,例如 selenium。

标签: python web-scraping scrapy web-crawler scrapy-spider


【解决方案1】:

正如@Shahin 提到的,页面是动态生成的,你无法解析它,没有像 selenium 或 splash 这样的东西。阅读this

还有另一种方法:您可以搜索请求的生成方式,这将为您提供所需的数据。

例如,当页面加载或进入底部时,向https://gql.twitch.tv/gql请求一些数据,请看下图:

此请求将返回带有目录游戏描述的 json: 因此,我认为您只需要了解如何构建请求数据并发出请求,而不是 twitch.tv/directory,而是 gql.twitch.tv/gql 并解析 json 格式的响应。

如何使用正文读取here(有正文参数)提出请求

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 2014-12-08
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多