【发布时间】:2018-06-08 22:50:03
【问题描述】:
我正在使用 scrapy 中的 python 蜘蛛来解析页面并且一切正常,除了从表中提取。它从表中提取数据,但重复多次。
所以对于表:
<tbody><tr>
<td><b>Name</b></td>
<td><b>Role</b></td>
</tr>
<tr style="background-color:#C0C0C0; color:Black;">
<td>Name 1</td>
<td>No Party</td>
</tr>
<tr style="background-color:#C0C0C0; color:Black;">
<td>Eny</td>
<td>Party</td>
</tr>
<tr style="background-color:#C0C0C0; color:Black;">
<td>Cory</td>
<td>Party</td>
</tr>
</tbody>
所以我需要从有第二方的表中获取字符串,将它们组合起来并写成一个字符串 我会这样写解析器:
raw_data['names'] = process_string(", ".join(page.xpath('//tr[td="Party"]/td[1]/text()').extract()))
我得到的结果:
埃尼,科里,恩特,科里,埃尼,科里,埃尼,科里
所以它重复了 4 次正确的结果。 纸币没有重复,其他领域都完美,所以纸币没有错,就是这个表达。 我尝试了不同的 xpath,但结果是一样的。表有什么不同?
【问题讨论】:
标签: python html scrapy scrapy-spider