【发布时间】:2014-12-11 18:43:30
【问题描述】:
我正在尝试抓取包含多行和具有相同类的数据的表的内容。这是网站:http://tweakers.net/pricewatch/323351/crucial-ballistix-tactical-blt2c4g3d1608et3lx0ceu/specificaties/
所以我的目标是从表类规范细节中获取品牌名称:Crucial
这是 HTML,所有 tr 都有相同的类,所以不可能用类来选择。
<div id="tab:specificaties" class="tab_active">
<table class="spec-detail">
<tbody>
<tr></tr>
<tr></tr>
<tr>
<td class="spec-index-column"></td>
<td class="spec-column">
<a href="http://tweakers.net/merk/306/crucial/">
Crucial
</a>
</td>
</tr>
<tr>
<td class="spec-index-column">
Serie
</td>
<td class="spec-column">
<a href="http://tweakers.net/serie/2930/ballistix-tactical/"></a>
</td>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
</tbody>
</table>
这是我的代码:
items = []
sel = Selector(response)
products = sel.xpath('//div[@id="tab:specificaties"]')
category = sel.xpath('//li[@id="tweakbaseBreadcrumbCategory"]/a/text()').extract()
print(category)
for product in products:
if 'Geheugen intern' in category:
item = Memory()
item['Category'] = category
item['Brand'] = ''.join(product.xpath('//tr[contains(td[1], "Merk")]/td[2]/a/text()').extract())
items.append(item)
返回
CrucialCrucialCrucial
我也试过
sel.xpath('//*[@id="tab:specificaties"]/table/tbody/tr[3]/td[2]/a/text()')
然而,这没有返回任何东西。
该选择可能与页面上的多个元素匹配,我找不到将它们分开的方法。我怎样才能使这个返回“Crucial”1 次?
提前致谢。
【问题讨论】:
标签: python-2.7 xpath web-scraping scrapy