python中用xpath解析html

匿名 (未验证) 提交于 2019-12-02 20:32:16

用xpath接下一个tbale,并提取值.

首先安装lxml包

pip3 install lxml 
from lxml import etree  # 获取返回响应的html文件 response = requests.get(url=url, cookies=cookie) # ElementTree对象 selector = etree.HTML(response.text) # 获得所有的tr trs = selector.xpath('//table[@class="tbl_type4"]/tbody/tr') # 遍历,提取每个td的值 for tr in trs:     td1 = tr.xpath('./td[1]/text()')[0].strip()     td2 = tr.xpath('./td[2]/a/text()')[0].strip()     td3 = tr.xpath('./td[3]/div/a/@title')[0].strip() 
文章来源: https://blog.csdn.net/weixin_43784462/article/details/91992394
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!