0

次のような HTML があります。

<html>
<body>
<table>
   <tr>
       Text before Text1
       <td>Text1</td>
       Text after Text1
   </tr>
   <tr>
       Text before Text2
       <td>Text2</td>
       Text after Text2
   </tr>
</table>
</body>
</html>

lxml と Python を使用しています。XPath を使用して検索Text after Text1し、Text after Text2

XPath を試し/html/body/table/trて相対パスのテキストを取得しようとしましたが、と./tdしか取得できません。Text before Text1Text before Text2

では、どうすればこれを達成できますか?

一例:

<tr>
  <td width="16"><img alt="" src="http://source.qunar.com/site/images/airlines/small/HU.gif"></td>
  <td valign="top">海航<span class="dc">HU7605</span><br>首都T1-虹桥/td>
</tr>

見つけられる海航けど見つからない首都T1-虹桥

4

2 に答える 2

1

ファイルがdata.xml.

from lxml import etree

data = etree.parse('data.xml')

for row in data.xpath('/html/body/table/tr'):
    before, after = row.xpath('text()')
    print before, after
于 2012-08-18T09:01:56.983 に答える
0

このようなXpath値を取得できます

             "//tr"  or "//tr/td"
于 2012-08-18T08:35:37.030 に答える