簡體   English   中英

如何在scrapy中正確使用XPATH?

[英]How do i use XPATH properly in scrapy?

http://mnregaweb4.nic.in/netnrega/asset_report_dtl.aspx?lflag=eng&state_name=WEST%20BENGAL&state_code=32&district_name=NADIA&district_code=3201&block_name=KRISHNAGAR-I&block_code=&panchayat_name=DOGACHI&panchayat_code=3201009009&fin_year=2020-2021&source=national&Digest=8+kWKUdwzDQA1IJ5qhD8Fw

以上是頁面的鏈接

: https://i.stack.imgur.com/8bhzV.png

紅色標記的框號是我試圖通過 xpath 獲得的

: https://i.stack.imgur.com/mca05.png

紅色標記的框是同一項目的檢查行。 我的代碼在下面


**scrapy shell**

**fetch("http://mnregaweb4.nic.in/netnrega/asset_report_dtl.aspx?lflag=eng&state_name=WEST%20BENGAL&state_code=32&district_name=NADIA&district_code=3201&block_name=KRISHNAGAR-I&block_code=&panchayat_name=DOGACHI&panchayat_code=3201009009&fin_year=2020-2021&source=national&Digest=8+kWKUdwzDQA1IJ5qhD8Fw")**

**assetid = response.xpath("//div[3]/center/table[2]/tbody/tr[4]/td[2]")**

**assetid**

**[]**(This is what it returns.)

**assetid = response.xpath("//div[3]/center/table[2]/tbody/tr[4]/td[2]/text()")**(I tried this also)

**assetid**

**[]**(This is what it returns.)

什么時候使用 view(response) 它說 true & 在瀏覽器中打開相同的頁面。

我的代碼在下面

: https://i.stack.imgur.com/YAf38.png

: https://i.stack.imgur.com/fTWwH.png

當您說結束 xpath 時,您將打印您期望的內容,我也會更新您的 xpath:

取而代之的是:

assetid = response.xpath("//div[3]/center/table[2]/tbody/tr[4]/td[2]")

用這個:

assetid = response.xpath('//table[2]//tr[4]/td[2]/text()').get()

我希望它會起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM