## **解析庫-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>