想要爬取网站上有关苹果病虫害的数据,但不知道为什么跑不出结果。
以下为结果
D:\ProgramData\Anaconda3\python.exe G:/apple/农业病虫草害多媒体数据库Xpath.py
进程已结束,退出代码 0
以下为代码
import requests
from lxml import etree
if __name__ == "__main__":
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.63'
}
url = 'http://www.chinawestagr.com/bch/Show.aspx?id=119&type=crop'
# url = 'http://www.chinawestagr.com/bch/Right.aspx'
page_text = requests.get(url=url,headers=headers).text
tree = etree.HTML(page_text)
tr_list = tree.xpath('/html/body/table[1]/tbody/tr/td/table/tbody/tr')
for tr in tr_list:
detail_url = 'http://www.chinawestagr.com/bch/'+tr.xpath('./td/a/@href')[0]
name = tr.xpath('./td/a/text()')[0]
print(detail_url,name)