[英]Scrapy: select texts of all children elements but returns children HTML as well
[英]Scrapy does not select sub-elements of the first child but of all children
有這個 HTML:
<table class="myTable>
<tbody>
<tr>A1</tr>
<tr>A2</tr>
</tbody>
<table>
<table class="myTable>
<tbody>
<tr>A1</tr>
<tr>A2</tr>
</tbody>
<table>
<table class="myTable>
<tbody>
<tr>A1</tr>
<tr>A2</tr>
</tbody>
<table>
我只想提取A1
和A2
一次。 所以我有這個選擇:
table = response.xpath('.//table[@class="myTable"]')[0]
row = table.xpath("//tr")
但是,當檢查len(row)
時,我得到 6,而不是 2,即使我檢查了len(table)
並且只得到 1(僅第一個表)。 那我該怎么做 select 呢?
您需要使用相對的 XPath:
row = table.xpath(".//tr")
或者您可以使用它來處理頁面上的第一個表格:
rows = response.xpath('(//table[@class="myTable"])[1]//tr')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.