我尝试从bandcamp.com的新版本中获取该页面的“发现”部分的项目标题(摇滚 - >所有摇滚 - >新来者)
scrapy shell 'https://bandcamp.com/?g=rock&s=new&p=0&gn=0&f=all&w=0'
页面的部分相关源代码如下所示:
<div class="col col-3-12 discover-item">
<a data-bind="click: playMe, css: { 'playing': playing }" class="item-link playable">
<span class="item-img ratio-1-1">
<img class="art" data-bind="src_art: { 'art_id': artId, 'format': 'art_tags_large' }" src="https://f4.bcbits.com/img/a1631562669_9.jpg">
<span class="plb-btn">
<span class="plb-bg"></span>
<span class="plb-ic"></span>
</span>
</span>
</a><a data-bind="attr: { 'href': itemURL }, text: title, click: playMe" class="item-title" href="https://reddieseloff.bandcamp.com/album/dead-rebel?from=discover-new">Dead Rebel</a>
<a data-bind="attr: { 'href': bandURL }, text: artist, click: playMe" class="item-artist" href="https://reddieseloff.bandcamp.com?from=discover-new">Red Diesel</a>
<span class="item-genre" data-bind="text: genre">rock</span>
</div>
我试图在xpath的帮助下得到item-title的文本(在这个例子中是'Dead Rebel'):
response.xpath('//div[@class="col col-3-12 discover-item"]//a[@class="item-title"]/text()').extract()
但它什么也没有返回。
[]
它也不适用于“项目艺术家”,所以我想知道我做错了什么。
我感谢任何帮助。
答案 0 :(得分:2)
您搜索的所有数据都隐藏在页面正文内隐藏的div
节点中
当您的浏览器加载网页时,javascript会指示如何解压缩并显示此数据,因为scrapy不运行任何javscript,您需要自己执行此步骤:
# all of the data is under "<div id="pagedata" data-blob=" attribute
data = response.css('div#pagedata::attr(data-blob)').extract()
import json
data = json.loads(data[0])
# dig through this python dictionary to find your data
(it has pretty much everything, even more than the page displays)