2016-05-02 9 views
1

私は治療とxpathを使用してデータを抽出したいと思います。これは私のコードです:属性x1,y1と属性x,y及びその他(line)と(image)最初:xpathの治療からの二重選択

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が2つの異なる要素が含まれていることです。私は取って、最終的なcsvを持ってそれらを一緒に入れようとしているが、私は正しいxpathを見つけることができませんどのように私はそれを解決することはできますか?

更新HTMLの2つの例:

<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> 

答えて

1

私が理解から、あなたはそれがyのためにそれ以外の場合は、同じにも存在し、x1場合xx属性として利用したいです。ここで私はそれを解決する方法を次のとおりです。

item['x'] = sel.xpath('@x').extract_first() or sel.xpath('@x1').extract_first() 
item['y'] = sel.xpath('@y').extract_first() or sel.xpath('@y1').extract_first() 

それとも、あなたは純粋なXPathのソリューション持つことができます。

item['x'] = sel.xpath('(@x|@x1)').extract_first() 
item['y'] = sel.xpath('(@y|@y1)').extract_first() 

をそして、あなたは両方のlineimage要素を処理する必要があるため、あなたがあなたのメインを調整する必要がありますそれを処理するための式:

//*[@id="pitch"]/*[contains(@class,"success")] 

または:

//*[@id="pitch"]/*[(self::image or self::line) and contains(@class,"success")] 
+0

申し訳ありません、私はhtml要素について間違っていました。私はちょうど私の答えを更新しました – slash89mf

+0

@ slash89mf大丈夫、更新を参照してください。助けてくれますか? – alecxe

+0

これは完璧です!ありがとうございました! – slash89mf