1

問題:
私は、scrapy-splash を使用して YouTube ビデオ ページをスクレイピングしています。ただし、要素を除いて、xpathは要素を返さないようkeywordsです。(xpath はすべて Chrome から直接コピーされます)

私が試したこと:
最初は、解析が呼び出されたときにページが完全に読み込まれていないためだと思ったので、SplashRequest の待機引数を変更しましたが、役に立ちませんでした。また、スプラッシュ GUI ( http://localhost:8050 )から html 応答のコピーをダウンロードし、ダウンロードしたコピーでxpath/selector がすべて正常に機能することを確認しました。ここで、この html はまさに Scrapy がパースで見るものであると仮定したので、なぜそれが Scrapy スクリプト内で機能しないのか理解できませんでした。

また、scrapy シェルを試してみましたが、すべて正常に動作します。
scrapy shell 'http://localhost:8050/render.html?url=https://www.youtube.com/watch?v=HOfTrhmIXIM&wait=2.0'

応答:

response.xpath('//*[@id="container"]/h1/yt-formatted-string/text()').extract_first(default='')                                                 
Out[2]: 'Scraping, analyzing youtube channel data with python'

コード:
これが私のコードです:

class videoSpider(scrapy.Spider):
name = "videoSpider"
start_urls = ["https://www.youtube.com/watch?v=HOfTrhmIXIM"]

def start_requests(self):
    for url in self.start_urls:
        yield SplashRequest(url=url, callback=self.parse, args={"wait":5})

def parse(self, response):
    item = YoutubeVideoItem()
    #print(response.text)
    item['keywords'] = response.xpath('/html/head/meta[@name="keywords"]/@content').extract_first(default='')
    item['title'] = response.xpath('//*[@id="container"]/h1/yt-formatted-string').extract_first(default='')
    item['category'] = response.xpath('//*[@id="content"]/yt-formatted-string/a').extract_first(default='')
    item['visualizations'] = response.xpath('//*[@id="count"]/yt-view-count-renderer/span[1]').extract_first(default='')
    item['publication_data'] = response.xpath('//*[@id="date"]/yt-formatted-string').extract_first(default='')
    yield item
4

1 に答える 1