【发布时间】:2016-05-02 14:46:36
【问题描述】:
我想使用 xpath 和 scrapy 提取数据。这是我的代码:
def parse(self, response):
Coords = []
for sel in response.xpath('//*[@id="pitch"]/image[contains(@class,"success")]'):
item = PogbaItem()
item['x'] = sel.xpath('@x').extract()
item['y'] = sel.xpath('@y').extract()
item['x'] = sel.xpath('@x1').extract()
item['y'] = sel.xpath('@y1').extract()
Coords.append(item)
return Coords
问题在于 html 包含两个不同的元素:第一个 (image) 具有属性 x,y,另一个 (line) 具有属性 x1,y1。我正在尝试将它们放在一起以获得最终的 csv,但我找不到正确的 xpath 我该如何解决?
更新:HTML的两个例子:
<image class="pitch-object timer-1-40 success" x="331.172" y="84.678" width="30" height="30" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/sites/fourfourtwo.com/modules/custom/statzone/files/icons/successful_clearance.png"></image>
<line class="pitch-object timer-2-84 success" marker-end="url(#smallblue)" x1="453.076" y1="199.169" x2="509.104" y2="216.676" style="stroke:blue;stroke-width:3"></line>
【问题讨论】: