Python Scrapy Xpath?
Python Scrapy Xpath?
为了一项非营利性大学作业,我正在尝试使用 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中第一行的内容)
//table[@class='artist_info']/tr[1]/td/text()
结果
'June 8, 1977,'
为了获得当前 运行 suhc xPage(table中第2行的内容)
//table[@class='artist_info']/tr[2]/td/a/text()
结果
'Hidden Hills, CA, United States'
为了一项非营利性大学作业,我正在尝试使用 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中第一行的内容)
//table[@class='artist_info']/tr[1]/td/text()
结果
'June 8, 1977,'
为了获得当前 运行 suhc xPage(table中第2行的内容)
//table[@class='artist_info']/tr[2]/td/a/text()
结果
'Hidden Hills, CA, United States'