您当前的位置: 首页 > 

默默爬行的虫虫

暂无认证

  • 1浏览

    0关注

    84博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

XPath总结笔记

默默爬行的虫虫 发布时间:2022-07-05 00:22:16 ,浏览量:1

接下来我们来讲提取细节,首先加载库

from lxml import etree

提取标签内容, /text()[0]或者/string()[0] 提取标签属性值, /@属性名 *表示任意节点 ,@*表示任何属性, node()表示任意节点

在这里插入图片描述

1. 解析html流程说明
url_02 = 'https://www.qdfd.com.cn/qdweb/realweb/fh/FhProjectInfo.jsp'
data_02 = {
   'projectID': shuzi_01}
response_02 = requests.post(url_02, data=data_02,headers=header)
if response.status_code == 200:
	response_02.encoding = 'GBK'
	sleep(random.uniform(0.2, 0.3))  # 生成一个a到b的小数等待时间
	# 请求是否成功
	# print(response_02.status_code)
	
	html_02 = etree.HTML(response_02.text)
	
	
	# #/html/body/div[1]/div[2]/ul[2]/table[2]/tbody/tr[position()>1]/td[2]/a
	shuzi_2 = html_02.xpath('/html/body/div[1]/div[2]/ul[2]//tr[position()>1]/td[2]/a')
a = '''标题

    
  • 列表1第1项
  • 列表1第2项

文字1

文字2

  • 列表2第1项
  • 列表2第2项
''' from lxml import etree html = etree.HTML(a) html.xpath('//title/text()')[0] # '标题' html.xpath("//p[@class='first']//text()")[0] # '文字1' html.xpath(
关注
打赏
1658895887
查看更多评论
0.0413s