无法弄清楚为什么我的 Scrapy 脚本不工作
Can't figure out why my Scrapy script isn't working
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 的新手,所以问题可能非常明显,但我无法弄清楚。
正如@Shahin 所提到的,页面是动态生成的,如果没有像 selenium 或 splash 这样的东西,你就无法解析它。阅读。
还有另一种方法:您可以搜索请求的生成方式,这将为您提供所需的数据。
例如,当页面加载或进入底部时,会向 https://gql.twitch.tv/gql 请求一些数据,请看下图:
这是请求 return 您 json 目录游戏描述:
所以,我认为您只需要找出请求数据是如何构建的,而不是 twitch.tv/directory
,而是 gql.twitch.tv/gql
并解析 json 格式的响应。
如何使用正文读取请求here(有正文参数)
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 的新手,所以问题可能非常明显,但我无法弄清楚。
正如@Shahin 所提到的,页面是动态生成的,如果没有像 selenium 或 splash 这样的东西,你就无法解析它。阅读
还有另一种方法:您可以搜索请求的生成方式,这将为您提供所需的数据。
例如,当页面加载或进入底部时,会向 https://gql.twitch.tv/gql 请求一些数据,请看下图:
这是请求 return 您 json 目录游戏描述:twitch.tv/directory
,而是 gql.twitch.tv/gql
并解析 json 格式的响应。
如何使用正文读取请求here(有正文参数)