我在使用xpath获取不一致的价目表时遇到问题
示例
<td><span="green">$33.99</span></td>
<td>Out of stock</td>
<td><span="green">$27.99</span></td>
<td><span="green">$35.00</span></td>
如何同时获得跨度内和缺货的价格? 因为我只得到33.99美元,或者任何有span的东西,而span中没有的文本都被跳过了。这破坏了秩序
我使用@piratefache的解决方案(Scrapy)更新的失败尝试
product_prices_tds = response.xpath('//td/')
product_prices = []
for td in product_prices_tds:
if td.xpath('//span'):
product_prices = td.xpath('//span/text()').extract()
else:
product_prices = td.xpath('//text()').extract()
for n in range(len(product_names)):
items['price'] = product_prices[n]
yield items
它不起作用,因为产品价格没有得到它从各地得到的正确文本。不只是像我想的那样在跨度内部或外部
更新 为了那个后来的人。多亏了@piratefache,我修复了我的代码。下面是更正后的代码片段,供以后使用
product_prices_tds = response.xpath('//td')
product_prices = []
for td in product_prices_tds:
if td.xpath('span'):
product_prices.append(td.xpath('span//text()').extract())
else:
product_prices.append(td.xpath('/text()').extract())
for n in range(len(product_names)):
items['price'] = product_prices[n]
yield items
请参见下面的“用刮片编辑”
根据您的html代码,使用BeautifulSoup库,您可以通过以下方式获取信息:
输出:
用刮痧:
输出:
XPath解决方案(从2.0开始)(与之前发布的@piratefache的逻辑相同):
应用于
返回
顺便说一句:
<span="green">
不是有效的XML。可能缺少属性@color或类似属性(?)相关问题 更多 >
编程相关推荐