gpt4 book ai didi

Python Scrapy Xpath?

转载 作者:行者123 更新时间:2023-12-01 04:24:47 25 4
gpt4 key购买 nike

对于一项非营利性大学作业,我正在尝试使用 python 中的 scrapy 框架从网站 www.rateyourmusic.com 抓取数据,我取得了一些成功,因为我能够抓取来自艺术家页面的艺术家,但其他信息(出生日期、国籍)的 xpath 对我来说很难抓取。你们中有人知道这些对象的正确 xpath 是什么吗?这是我的解析方法,至少适用于艺术家名称。

def parse_dir_contents(self, response):
item = rateyourmusicartist()

for sel in response.xpath('//div/div/div/div/table/tbody/tr/td'):
item['dateofbirth'] = sel.xpath('td/text()').extract() #these two selectors aren't working
item['nationality'] = sel.xpath('td/a/text()').extract()

for sel in response.xpath('//div/div/div/div/div/h1'):
item['name'] = sel.xpath('text()').extract() #this is the one that works

yield item

这是我正在抓取的艺术家页面的示例 URL http://rateyourmusic.com/artist/kanye_west

最佳答案

这是页面上的真实 HTML 片段(如果您将页面作为源打开,则可以看到它)。

<table class="artist_info">
<tr><td><div class="info_hdr">Born</div> June 8, 1977, <a class="location" href="/location/Atlanta/GA/United States">Atlanta, GA, United States</a></td></tr>
<tr><td><div class="info_hdr">Currently</div><a class="location" href="/location/Hidden Hills/CA/United States">Hidden Hills, CA, United States</a></td></tr>
</table>

为了获取生日,运行suhc xPage(表中第一行的内容)

//table[@class='artist_info']/tr[1]/td/text()

结果

'June 8, 1977,'

为了获取当前运行suhc xPage(表中第二行的内容)

//table[@class='artist_info']/tr[2]/td/a/text()

结果

'Hidden Hills, CA, United States'

关于Python Scrapy Xpath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33281461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com