我是 scrapy 新手,我正在尝试从嵌套表内图像的标题属性中获取文本值。下面是一个表格示例
<html>
<body>
<div id=yw1>
<table id="x">
<thead></thead>
<tbody>
<tr>
<td>
<table id="y">
<thead></thead>
<tbody>
<tr>
<td><img src=".." title="Sample"></td>
<td></td>
</tr>
</tbody>
</table>
</td>
<td></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
我使用以下 scrapy 代码从 title 属性获取文本。
def parse(self, response):
transfers = Selector(response).xpath('//*[@id="yw1"]/table/tbody/tr')
for transfer in transfers:
item = TransfermarktItem()
item['naam'] = transfer.xpath('td[1]/table/tbody/tr[1]/td[1]/img/@title/text()').extract()
item['positie'] = transfer.xpath('td[1]/table/tbody/tr[1]/td[2]/a/text()').extract()
item['leeftijd'] = transfer.xpath('td[2]/text()').extract()
yield item
由于某种原因,未提取标题属性的文本值。我做错了什么??
干杯!
最佳答案
看来你可以使用
item['naam'] = transfer.xpath(
'td[1]/table/tbody/tr[1]/td[1]/img/@title'
)
这将返回一个列表。
text()
对于获取标签属性值没有用处。
extract()
我想这里也可以省略。
编辑:
如果上述方法仍然不起作用,则还有更多可能是 tbody
问题,即 http://doc.scrapy.org/en/latest/topics/firefox.html 。你可以这样尝试:
td[1]/table//tr[1]/td[1]/img/@title
如果这没有帮助,那么根据我们在这里得到的数据,我想我已经没有主意了:)
关于python - scrapy从嵌套表内的图像标题属性获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34643664/