0
PythonのlxmlによるXPathは私をサークルで動かすようにしています。私は、正しいXPathであると信じているものの、HTMLテーブルからテキストを抽出することはできません。私は、XPathを調べて抽出するためにChromeを使用しています。それを自分のコードで使用しています。ここでPython XPathは空のリストを返し続けます
がページから直接取らHTMLテーブルです:
<div id="vehicle-detail-model-specs-container">
<table id="vehicle-detail-model-specs" class="table table-striped vdp-feature-table">
<!-- Price -->
<tr>
<td><strong>Price:</strong></td>
<td>
<strong id="vehicle-detail-price" itemprop="price">$ 2,210.00</strong> </td>
</tr>
<!-- VIN -->
<tr><td><strong>VIN</strong></td><td> *0343</td></tr>
<!-- MILEAGE -->
<tr><td><strong>Mileage</strong></td><td>0 mi</td></tr>
</table>
私はマイレージを抽出しようとしています。
//*[@id="vehicle-detail-model-specs"]/tbody/tr[3]/td[2]
そして、私が使用しているPythonのコードは次のとおりです:私が使用しているXPathはある
page = requests.get(URL)
tree = html.fromstring(page.content)
mileage = tree.xpath('//*[@id="vehicle-detail-model-specs"]/tbody/tr[3]/td[2]')
print mileage
注:私は最後まで/text()
を追加しようとしたと私はまだ戻って何も得ます、ちょうど空のリスト[]。
私は間違っていますが、なぜ上記の例からテーブルの値を抽出できないのですか?
は、あなたは、XPathから 'tbody'を省略しようとしましたか? – Amber
https://stackoverflow.com/a/18241030/407651 – mzjn
「私のXPathクエリ(HTMLテーブルのスクラップ)はFirebugでしか動作しませんが、私が開発しているアプリケーションでは動作しません」という複製がありますか?](https:// stackoverflow.com/questions/18241029/why-does-my-xpath-query-scraping-html-tables-only-work-in-firebug-but-not-the) – Andersson