企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## **解析库-XPath实战与工具的使用** 导读:XPath的常用方法,已经完结,我们也具备了提取文本的基本能力。让我们一起尝试面对复杂的文本情况如何进行提取。 ### <span style="color:red">实战目标</span>:京东商品列表 <br> [https://search.jd.com/Search?keyword=Python&enc=utf-8&wq=Python&pvid=f91d6934996a429baacf46a0408bf352](https://search.jd.com/Search?keyword=Python&enc=utf-8&wq=Python&pvid=f91d6934996a429baacf46a0408bf352) <img src="https://gitee.com/Wiliam01/img_all/raw/master/%E4%B9%A6%E7%B1%8D%E5%88%97%E8%A1%A8.png" title="待爬取信息"> <br> ### **1.待爬取信息:** - 图片列表 - 价格 - 书籍名称 - 出版社 - 评价数量 <br> ### **2.请求,获取页面信息:** ~~~ import requests url = "https://search.jd.com/Search?keyword=Python&enc=utf-8&wq=Python&pvid=f91d6934996a429baacf46a0408bf352" headers = { "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" } response = requests.get(url,headers=headers) # 设置网页编码,response.apparent_encoding 的作用是猜测当前网页的编码格式 response.encoding = response.apparent_encoding print(response.text) ~~~ <br> ### **3.分析,提取有用信息:** 1.定位数据位置。 2.发现数据元素周围特征。 3.书写xpath规则进行提取。 <img src="https://gitee.com/Wiliam01/img_all/raw/master/%E9%A1%B5%E9%9D%A2%E5%88%86%E6%9E%90.png" title="页面分析"></img> 由图我们可以看到数据全部在ul 的列表中,所以我们先要获取所有li标签,然后根据li标签的位置来进行提取对应数据 ~~~ with open("1.html","r+",encoding="utf8") as f: res = f.read() html = etree.HTML(res) # 获取所有的li标签 li_res = html.xpath('//*[@id="J_goodsList"]/ul/li') for e,li in enumerate(li_res): title = li.xpath('//li[{}]//div[@class="p-name"]/a/@title'.format(e+1)) title_url = li.xpath('//li[{}]//div[@class="p-name"]/a/@href'.format(e+1)) price = li.xpath('//li[{}]//div[@class="p-price"]//i/text()'.format(e+1)) img_url = li.xpath('//li[{}]//div[@class="p-img"]//img/@source-data-lazy-img'.format(e+1)) commit = li.xpath('//li[{}]//div[@class="p-commit"]//a/@id'.format(e+1)) shopnum = li.xpath('//li[{}]//div[@class="p-shopnum"]//a/text()'.format(e+1)) print(title,title_url,price,img_url,commit,shopnum) input() 输出结果: ['Python3.5编程入门图书,机器学习,数据处理,网络爬虫热门编程语言,从基本概念到完整项目开发,帮助零基础读者迅速掌握Python编程,附赠源代码文件'] ['//item.jd.com/11993134.html'] ['74.50'] ['//img14.360buyimg.com/n1/s200x200_jfs/t17953/201/1450663539/451183/3262b8de/5acb3627N8191c867.jpg'] ['J_comment_11993134'] ['人民邮电出版社'] ~~~ 这样的话,我们就获取到了所有的内容,因为评论数的接口是动态获取的,所以我们并不能直接获取,这个就是评论数的接口,我们只需要获取评论数的id即可请求获取评论数。 或许你会问我如何获取接口:首先判断是不是动态加载,也就是ajax加载,很明显,我们查看源码会发现,我们在div class="p-commit" 标签中并没有看到数据。所以我们通过搜素js内容,发现了js代码做了处理,如下: <img src="https://gitee.com/Wiliam01/img_all/raw/master/js%E5%8A%A0%E5%AF%86.png" title=“js部分”> 通过分析js代码,我们得知接口如下: https://club.jd.com/comment/productCommentSummaries.action?referenceIds=11993134 注:以上就是我们提取信息部分。 <hr> > 工欲善其事必先利其器,所以我们将介绍快速上手的一下快捷工具和方法! 1.浏览器自带的xpath复制。 <img src="https://gitee.com/Wiliam01/img_all/raw/master/%E9%A1%B5%E9%9D%A2%E6%88%AA%E5%8F%96.png"> 菜单选项:Copy -> Copy Xpath 2.浏览器插件: - 谷歌浏览器安装xpath插件(注意,需要科学上网) <img src="https://gitee.com/Wiliam01/img_all/raw/master/xpath%E6%8F%92%E4%BB%B6.png"></img> - 使用方法: <img src="https://gitee.com/Wiliam01/img_all/raw/master/xpath%E4%BD%BF%E7%94%A8.png"></img>