[英]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請求一些數據,請看下圖:
這是請求將返回帶有目錄游戲描述的 json: 所以,我認為您只需要找出請求數據如何構建和發出請求,而不是
twitch.tv/directory
,而是gql.twitch.tv/gql
並解析 json 格式的響應。
如何在此處使用正文發出請求(有正文參數)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.