简体   繁体   中英

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

Above is the link of the page

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

The red marked box number is what I am trying to get through xpath

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

The red marked box is the inspect line of that same item. my code is below


**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.)

when is use view(response) it says true & opens the same page in browser.

My code is below

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

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

When you say get end of the xpath you will print what you expect also I update your xpath too:

instead this:

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

use this:

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

I hope it will works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM