Python的xpath数据解析案例分析(python,xpath,开发技术)

时间:2024-05-03 09:54:17 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

xpath基本概念

xpath解析:最常用且最便捷高效的一种解析方式。通用性强。

xpath解析原理

1.实例化一个etree的对象,且需要将被解析的页面源码数据加载到该对象中

2.调用etree对象中的xpath方法结合xpath表达式实现标签的定位和内容的捕获。

环境安装

pipinstalllxml

如何实例化一个etree对象

fromlxmlimportetree

1.将本地的html文件中的远吗数据加载到etree对象中:

etree.parse(filePath)

2.可以将从互联网上获取的原码数据加载到该对象中:

etree.HTML(‘page_text')

xpath(‘xpath表达式’)

1./:表示的是从根节点开始定位。表示一个层级

2.//:表示多个层级。可以表示从任意位置开始定位

3.属性定位://div[@class='song'] tag[@attrName='attrValue']

4.索引定位://div[@class='song']/p[3] 索引从1开始的

5.取文本:

  • /text()获取的是标签中直系的文本内容

  • //text()标签中非直系的文本内容(所有文本内容)

6.取属性:/@attrName ==>img/src

xpath爬取58二手房实例

完整代码

fromlxmlimportetreeimportrequestsif__name__=='__main__':headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/84.0.4147.105Safari/537.36'}url='https://xa.58.com/ershoufang/'page_text=requests.get(url=url,headers=headers).texttree=etree.HTML(page_text)div_list=tree.xpath('//section[@class="list"]/div')fp=open('./58同城二手房.txt','w',encoding='utf-8')fordivindiv_list:title=div.xpath('.//div[@class="property-content-title"]/h4/text()')[0]print(title)fp.write(title+'\n'+'\n')

Python的xpath数据解析案例分析

xpath图片解析下载实例

完整代码

importrequests,osfromlxmlimportetreeif__name__=='__main__':headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/84.0.4147.105Safari/537.36'}url='https://pic.netbian.com/4kmeinv/'page_text=requests.get(url=url,headers=headers).texttree=etree.HTML(page_text)li_list=tree.xpath('//div[@class="slist"]/ul/li/a')ifnotos.path.exists('./piclibs'):os.mkdir('./piclibs')forliinli_list:detail_url='https://pic.netbian.com'+li.xpath('./img/@src')[0]detail_name=li.xpath('./img/@alt')[0]+'.jpg'detail_name=detail_name.encode('iso-8859-1').decode('GBK')detail_path='./piclibs/'+detail_namedetail_data=requests.get(url=detail_url,headers=headers).contentwithopen(detail_path,'wb')asfp:fp.write(detail_data)print(detail_name,'seccess!!')

Python的xpath数据解析案例分析

xpath爬取全国城市名称实例

完整代码

importrequestsfromlxmlimportetreeif__name__=='__main__':url='https://www.aqistudy.cn/historydata/'headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/87.0.4280.141Safari/537.36',}page_text=requests.get(url=url,headers=headers).content.decode('utf-8')tree=etree.HTML(page_text)#热门城市//div[@class="bottom"]/ul/li#全部城市//div[@class="bottom"]/ul/div[2]/lia_list=tree.xpath('//div[@class="bottom"]/ul/li|//div[@class="bottom"]/ul/div[2]/li')fp=open('./citys.txt','w',encoding='utf-8')i=0foraina_list:city_name=a.xpath('.//a/text()')[0]fp.write(city_name+'\t')i=i+1ifi==6:i=0fp.write('\n')print('爬取成功')

Python的xpath数据解析案例分析

xpath爬取简历模板实例

完整代码

importrequests,osfromlxmlimportetreeif__name__=='__main__':url='https://sc.chinaz.com/jianli/free.html'headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/87.0.4280.141Safari/537.36',}page_text=requests.get(url=url,headers=headers).content.decode('utf-8')tree=etree.HTML(page_text)a_list=tree.xpath('//div[@class="boxcol3ws_block"]/a')ifnotos.path.exists('./简历模板'):os.mkdir('./简历模板')foraina_list:detail_url='https:'+a.xpath('./@href')[0]detail_page_text=requests.get(url=detail_url,headers=headers).content.decode('utf-8')detail_tree=etree.HTML(detail_page_text)detail_a_list=detail_tree.xpath('//div[@class="clearfixmt20downlist"]/ul/li[1]/a')foraindetail_a_list:download_name=detail_tree.xpath('//div[@class="ppt_titclearfix"]/h2/text()')[0]download_url=a.xpath('./@href')[0]download_data=requests.get(url=download_url,headers=headers).contentdownload_path='./简历模板/'+download_name+'.rar'withopen(download_path,'wb')asfp:fp.write(download_data)print(download_name,'success!!')

Python的xpath数据解析案例分析

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Python的xpath数据解析案例分析的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:spring data JPA中的多属性排序方法是什么下一篇:

2 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18