如何在scrapy中正确使用XPATH?

2024-07-05 15:25:00 发布

您现在位置:Python中文网/ 问答频道 /正文

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

当使用视图(响应)时,它会显示true&;在浏览器中打开同一页

我的代码在下面

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

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


Tags: namehttpscomhttppngstackcodeblock
1条回答
网友
1楼 · 发布于 2024-07-05 15:25:00

当您说get end of 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()

我希望它能起作用

相关问题 更多 >