2016-09-06 4 views
2

と表の左端の列からのhrefを取得し、私はここに、テーブルからhrefテキストを抽出しようとしています:ここでhttps://en.wikipedia.org/wiki/List_of_first-person_shootersは、XPath

は、テーブルの上です:

<table class="wikitable sortable" style="font-size: 85%; text-align: left;"> 
<tr style="background: #ececec"> 
<th>Title</th> 
<th>Developer</th> 
<th>Platform(s)</th> 
<th>Release Date</th> 
</tr> 
<tr> 
<th><i><a href="/wiki/007_Legends" title="007 Legends">007 Legends</a></i></th> 
<td><a href="/wiki/Eurocom" title="Eurocom">Eurocom</a>, <a href="/wiki/Activision" title="Activision">Activision</a></td> 
<td>PS3, X360, Wii U, WIN</td> 
<td>2012-10-16</td> 
</tr> 
<tr> 
<th><i><a href="/wiki/007:_Quantum_of_Solace" title="007: Quantum of Solace">007: Quantum of Solace</a></i></th> 
<td><a href="/wiki/Treyarch" title="Treyarch">Treyarch</a>, <a href="/wiki/Beenox" title="Beenox">Beenox</a></td> 
<td>DS, PS3, Wii, WIN, X360</td> 
<td>2008-10-31</td> 
</tr> 
<tr> 
<th><i><a href="/wiki/3D_Monster_Chase" title="3D Monster Chase">3D Monster Chase</a></i></th> 
<td><a href="/w/index.php?title=Romik&amp;action=edit&amp;redlink=1" class="new" title="Romik (page does not exist)">Romik</a></td> 
<td>AMSCPC, ZX</td> 
<td>1985</td> 
</tr> 

次のXPathクエリは取得しますテーブルからのhrefテキストですが、各行の最初の列のみが必要です。これはXPathで可能ですか?カウンターなしで可能ですか?私は、Pythonライブラリlxmlを使用しています:

tree.xpath('//table[@class="wikitable sortable"]//a/@href') 

は取得:

['/wiki/007_Legends', '/wiki/Eurocom', '/wiki/Activision', '/wiki/007:_Quantum_of_Solace', '/wiki/Treyarch', '/wiki/Beenox', '/wiki/3D_Monster_Chase', '/w/index.php?title=Romik&action=edit&redlink=1', '/wiki/Ace_of_Spades_(video_game)', '/w/index.php?title=Ben_Aksoy&action=edit&redlink=1', '/wiki/Alcatraz:_Prison_Escape', '/wiki/Zombie_Studios', '/wiki/CodeRED:_Alien_Arena', '/w/index.php?title=COR_Entertainment&action=edit&redlink=1', '/wiki/FreeBSD', '/wiki/Alien_Breed_3D', '/wiki/Team17', '/wiki/Alien_Breed_3D_II:_The_Killing_Grounds', '/wiki/Team17', 

しかし、私は、各行の最初の項目

答えて

1

私はそれぞれの行から1列

このXPathをしたい、

//table[@class="wikitable sortable"]//tr/*[1]//a/@href 

は、各trの最初の列で見つかっただけa/@hrefを選択します。

/wiki/007_Legends 
/wiki/007:_Quantum_of_Solace 
/wiki/3D_Monster_Chase 
最初の列が tdthかどうかにかかわらず、

になります。

あなたはtdエントリでのみ興味があるなら、あなたは、td

//table[@class="wikitable sortable"]//tr/td[1]//a/@href 

*を置き換えることができ、あなたは、これらの値を持つa/@href属性を選択します:

/wiki/Eurocom 
/wiki/Activision 
/wiki/Treyarch 
/wiki/Beenox 
/w/index.php?title=Romik&action=edit&redlink=1 
-1

のみ最初の列の使用<th><i>ので、それは

使用したいと思います
tree.xpath('//table[@class="wikitable sortable"]//th//a/@href') 

または

tree.xpath('//table[@class="wikitable sortable"]//i/a/@href')