企业网站建设信息管理平台做网站外包公司名称
news/
2025/10/9 1:00:06/
文章来源:
企业网站建设信息管理平台,做网站外包公司名称,网站建设 教学视频教程,wordpress添加文档本篇文章主要介绍了python爬虫之xpath的基本使用详解#xff0c;现在分享给大家#xff0c;也给大家做个参考。一起过来看看吧一、简介XPath 是一门在 XML 文档中查找信息的语言。XPath 可用来在 XML 文档中对元素和属性进行遍历。XPath 是 W3C XSLT 标准的主要元素#xff…本篇文章主要介绍了python爬虫之xpath的基本使用详解现在分享给大家也给大家做个参考。一起过来看看吧一、简介XPath 是一门在 XML 文档中查找信息的语言。XPath 可用来在 XML 文档中对元素和属性进行遍历。XPath 是 W3C XSLT 标准的主要元素并且 XQuery 和 XPointer 都构建于 XPath 表达之上。二、安装pip3 install lxml三、使用1、导入from lxml import etree2、基本使用from lxml import etreewb_data first itemsecond itemthird itemfourth itemfifth itemhtml etree.HTML(wb_data)print(html)result etree.tostring(html)print(result.decode(utf-8))从下面的结果来看我们打印机html其实就是一个python对象etree.tostring(html)则是不全里html的基本写法补全了缺胳膊少腿的标签。first itemsecond itemthird itemfourth itemfifth item3、获取某个标签的内容(基本使用)注意获取a标签的所有内容a后面就不用再加正斜杠否则报错。写法一html etree.HTML(wb_data)html_data html.xpath(/html/body/p/ul/li/a)print(html)for i in html_data:print(i.text)first itemsecond itemthird itemfourth itemfifth item写法二(直接在需要查找内容的标签后面加一个/text()就行)html etree.HTML(wb_data)html_data html.xpath(/html/body/p/ul/li/a/text())print(html)for i in html_data:print(i)first itemsecond itemthird itemfourth itemfifth item4、打开读取html文件#使用parse打开html的文件html etree.parse(test.html)html_data html.xpath(//*)#打印是一个列表需要遍历print(html_data)for i in html_data:print(i.text)html etree.parse(test.html)html_data etree.tostring(html,pretty_printTrue)res html_data.decode(utf-8)print(res)打印first itemsecond itemthird itemfourth itemfifth item5、打印指定路径下a标签的属性(可以通过遍历拿到某个属性的值查找标签的内容)html etree.HTML(wb_data)html_data html.xpath(/html/body/p/ul/li/a/href)for i in html_data:print(i)打印link1.htmllink2.htmllink3.htmllink4.htmllink5.html6、我们知道我们使用xpath拿到得都是一个个的ElementTree对象所以如果需要查找内容的话还需要遍历拿到数据的列表。查到绝对路径下a标签属性等于link2.html的内容。html etree.HTML(wb_data)html_data html.xpath(/html/body/p/ul/li/a[hreflink2.html relexternal nofollow relexternal nofollow relexternal nofollow relexternal nofollow relexternal nofollow ]/text())print(html_data)for i in html_data:print(i)打印[second item]second item7、上面我们找到全部都是绝对路径(每一个都是从根开始查找)下面我们查找相对路径例如查找所有li标签下的a标签内容。html etree.HTML(wb_data)html_data html.xpath(//li/a/text())print(html_data)for i in html_data:print(i)打印[first item, second item, third item, fourth item, fifth item]first itemsecond itemthird itemfourth itemfifth item8、上面我们使用绝对路径查找了所有a标签的属性等于href属性值利用的是/---绝对路径下面我们使用相对路径查找一下l相对路径下li标签下的a标签下的href属性的值注意a标签后面需要双//。html etree.HTML(wb_data)html_data html.xpath(//li/a//href)print(html_data)for i in html_data:print(i)打印[link1.html, link2.html, link3.html, link4.html, link5.html]link1.htmllink2.htmllink3.htmllink4.htmllink5.html9、相对路径下跟绝对路径下查特定属性的方法类似也可以说相同。html etree.HTML(wb_data)html_data html.xpath(//li/a[hreflink2.html relexternal nofollow relexternal nofollow relexternal nofollow relexternal nofollow relexternal nofollow ])print(html_data)for i in html_data:print(i.text)打印[]second item10、查找最后一个li标签里的a标签的href属性html etree.HTML(wb_data)html_data html.xpath(//li[last()]/a/text())print(html_data)for i in html_data:print(i)打印[fifth item]fifth item11、查找倒数第二个li标签里的a标签的href属性html etree.HTML(wb_data)html_data html.xpath(//li[last()-1]/a/text())print(html_data)for i in html_data:print(i)打印[fourth item]fourth item12、如果在提取某个页面的某个标签的xpath路径的话可以如下图//*[idkw]解释使用相对路径查找所有的标签属性id等于kw的标签。常用#!/usr/bin/env python# -*- coding:utf-8 -*-from scrapy.selector import Selector, HtmlXPathSelectorfrom scrapy.http import HtmlResponsehtml first itemfirst itemsecond itemvvsecond itemresponse HtmlResponse(urlhttp://example.com, bodyhtml,encodingutf-8)# hxs HtmlXPathSelector(response)# print(hxs)# hxs Selector(responseresponse).xpath(//a)# print(hxs)# hxs Selector(responseresponse).xpath(//a[2])# print(hxs)# hxs Selector(responseresponse).xpath(//a[id])# print(hxs)# hxs Selector(responseresponse).xpath(//a[idi1])# print(hxs)# hxs Selector(responseresponse).xpath(//a[hreflink.html relexternal nofollow relexternal nofollow ][idi1])# print(hxs)# hxs Selector(responseresponse).xpath(//a[contains(href, link)])# print(hxs)# hxs Selector(responseresponse).xpath(//a[starts-with(href, link)])# print(hxs)# hxs Selector(responseresponse).xpath(//a[re:test(id, i\d)])# print(hxs)# hxs Selector(responseresponse).xpath(//a[re:test(id, i\d)]/text()).extract()# print(hxs)# hxs Selector(responseresponse).xpath(//a[re:test(id, i\d)]/href).extract()# print(hxs)# hxs Selector(responseresponse).xpath(/html/body/ul/li/a/href).extract()# print(hxs)# hxs Selector(responseresponse).xpath(//body/ul/li/a/href).extract_first()# print(hxs)# ul_list Selector(responseresponse).xpath(//body/ul/li)# for item in ul_list:# v item.xpath(./a/span)# # 或# # v item.xpath(a/span)# # 或# # v item.xpath(*/a/span)# print(v)相关推荐
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/932104.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!