Python使用xpath對解析內(nèi)容進(jìn)行數(shù)據(jù)提取
一、前言
在前面的文章當(dāng)中,已經(jīng)教大家如何去獲取我們需要的數(shù)據(jù)原文內(nèi)容,今天就介紹一個(gè)用于提取所需數(shù)據(jù)的方法之一xpath。在后續(xù)會講解bs4(beautifulsoup),re正則表達(dá)式。
二、正文
XPath 使用路徑表達(dá)式來選取HTML/ XML 文檔中的節(jié)點(diǎn)或節(jié)點(diǎn)集。節(jié)點(diǎn)是通過沿著路徑 (path) 或者步 (steps) 來選取的。
使用到python中的一個(gè)lxml庫:下載 pip install lxml
選取節(jié)點(diǎn)
| 表達(dá)式 | 描述 |
|---|---|
| nodename | 選取此節(jié)點(diǎn)的所有子節(jié)點(diǎn)。 |
| / | 從根節(jié)點(diǎn)選取(取子節(jié)點(diǎn))。 |
| // | 從匹配選擇的當(dāng)前節(jié)點(diǎn)選擇文檔中的節(jié)點(diǎn),而不考慮它們的位置(取子孫節(jié)點(diǎn))。 |
| . | 選取當(dāng)前節(jié)點(diǎn)。 |
| .. | 選取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)。 |
| @ | 選取屬性。 |
路徑表達(dá)式
| 路徑表達(dá)式 | 結(jié)果 |
|---|---|
| bookstore | 選取 bookstore 元素的所有子節(jié)點(diǎn)。 |
| /bookstore | 選取根元素 bookstore。注釋:假如路徑起始于正斜杠( / ),則此路徑始終代表到某元素的絕對路徑! |
| bookstore/book | 選取屬于 bookstore 的子元素的所有 book 元素。 |
| //book | 選取所有 book 子元素,而不管它們在文檔中的位置。 |
| bookstore//book | 選擇屬于 bookstore 元素的后代的所有 book 元素,而不管它們位于 bookstore 之下的什么位置。 |
| //@lang | 選取名為 lang 的所有屬性。 |
謂語
謂語用來查找某個(gè)特定的節(jié)點(diǎn)或者包含某個(gè)指定的值的節(jié)點(diǎn)。
謂語被嵌在方括號中。
| 路徑表達(dá)式 | 結(jié)果 |
|---|---|
| /bookstore/book[1] | 選取屬于 bookstore 子元素的第一個(gè) book 元素。 |
| /bookstore/book[last()] | 選取屬于 bookstore 子元素的最后一個(gè) book 元素。 |
| /bookstore/book[last()-1] | 選取屬于 bookstore 子元素的倒數(shù)第二個(gè) book 元素。 |
| /bookstore/book[position()<3] | 選取最前面的兩個(gè)屬于 bookstore 元素的子元素的 book 元素。 |
| //title[@lang] | 選取所有擁有名為 lang 的屬性的 title 元素。 |
| //title[@lang='eng'] | 選取所有 title 元素,且這些元素?fù)碛兄禐?eng 的 lang 屬性。 |
選取未知節(jié)點(diǎn)
| 通配符 | 描述 |
|---|---|
| * | 匹配任何元素節(jié)點(diǎn)。 |
| @* | 匹配任何屬性節(jié)點(diǎn)。 |
| node() | 匹配任何類型的節(jié)點(diǎn)。 |
--- 在下面的表格中,我們列出了一些路徑表達(dá)式,以及這些表達(dá)式的結(jié)果:
| 路徑表達(dá)式 | 結(jié)果 |
|---|---|
| /bookstore/* | 選取 bookstore 元素的所有子元素。 |
| //* | 選取文檔中的所有元素。 |
| //title[@*] | 選取所有帶有屬性的 title 元素。 |
選取若干節(jié)點(diǎn)
通過在路徑表達(dá)式中使用"|"運(yùn)算符,您可以選取若干個(gè)路徑。
| 路徑表達(dá)式 | 結(jié)果 | |
|---|---|---|
| //book/title | //book/price | 選取 book 元素的所有 title 和 price 元素。 |
| //title | //price | 選取文檔中的所有 title 和 price 元素。 |
| /bookstore/book/title | //price | 選取屬于 bookstore 元素的 book 元素的所有 title 元素,以及文檔中所有的 price 元素。 |
三、示例
下面給出一個(gè)示例代碼
# -*- coding:utf-8 -*-
import requests
from lxml import etree
?
?
class DouGuo(object):
def __init__(self):
self.url = "https://www.douguo.com/caipu/%E5%AE%B6%E5%B8%B8%E8%8F%9C/0/20"
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
}
?
def get_data_index(self):
response = requests.get(self.url, headers=self.headers)
response.encoding="utf-8"
if response.status_code == 200:
return response.text
else:
return None
?
def parse_data_index(self, response):
html = etree.HTML(response)
data_list = html.xpath('//ul[@class="cook-list"]//li[@class="clearfix"]')
for data in data_list:
# 提取文本值
title = data.xpath("./div/a/text()")[0]
major = data.xpath("./div/p/text()")[0]
# 提取屬性值
head = data.xpath("./div/div[2]/a/img/@alt")[0]
score = data.xpath("./div/div[1]//span/text()")[0]
print(f"title: {title}\nmajor: {major}\nhead:{head}\nscore:{score}\n\n")
?
def run(self):
response = self.get_data_index()
# print(response)
self.parse_data_index(response)
?
if __name__ == '__main__':
spider = DouGuo()
spider.run()
四、結(jié)語
大家可以嘗試去抓取這個(gè)url https://cs.lianjia.com/ershoufang/
獲取第一頁數(shù)據(jù)即可,同時(shí)也可以思考一下,如何進(jìn)行多頁的獲取,實(shí)現(xiàn)翻頁功能。
到此這篇關(guān)于Python使用xpath對解析內(nèi)容進(jìn)行數(shù)據(jù)提取的文章就介紹到這了,更多相關(guān)Python xpath數(shù)據(jù)提取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)戰(zhàn)之生成有關(guān)聯(lián)單選問卷
這篇文章主要為大家分享了一個(gè)Python實(shí)戰(zhàn)小案例——生成有關(guān)聯(lián)單選問卷,并且能根據(jù)問卷總分?jǐn)?shù)生成對應(yīng)判斷文案結(jié)果,感興趣的可以了解一下2023-04-04
python 統(tǒng)計(jì)列表中不同元素的數(shù)量方法
今天小編就為大家分享一篇python 統(tǒng)計(jì)列表中不同元素的數(shù)量方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
python 利用pyttsx3文字轉(zhuǎn)語音過程詳解
這篇文章主要介紹了python 利用pyttsx3文字轉(zhuǎn)語音過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
在Python3.74+PyCharm2020.1 x64中安裝使用Kivy的詳細(xì)教程
這篇文章主要介紹了在Python3.74+PyCharm2020.1 x64中安裝使用Kivy的詳細(xì)教程,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
pytorch之關(guān)于PyTorch結(jié)構(gòu)介紹
這篇文章主要介紹了pytorch之關(guān)于PyTorch結(jié)構(gòu)的使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

