我們在寫爬蟲的過程中,除了研究反爬之外,幾乎全部的時間都在寫解析邏輯。那么,生命苦短,為什么我們不寫一個通用解析器呢?對??!為什么不呢?開整!
需求分析
爬蟲要解析的網頁類型無外乎 html、json 以及一些二進制文件(video、excel 文件等)。既然要做成通用解析器,我們有兩種實現方式,一種是將網頁內容轉換成統一的形式,然后用對應的解析規則去解析,比如全部將網頁內容轉換成 html 形式,然后用 xpath 去提取。

另外一種是配置文件預先告知的方式,你配置成什么類型,解析器就通過對應的解析規則去解析。
統一網頁形式,需要做大量的網頁內容形式轉換,而配置文件預先告知則需要在配置時指定更多解析字段。相比較而言,通過第二種方式,未來改變較多的是配置規則,不需要動核心代碼,引入 bug 的可能性較低。因此這里我們采用第二種方式實現解析器
進一步分析
解析器對于網頁內容的提取,本質上和我們在本地電腦上查找和整理文件,沒有什么差別。比如像下面這樣

解析內容就是從中提取我們想要的信息,然后整理成我們希望的格式。比如上面的內容,我們提取出來的形式應該是這樣
{
"design": "設計圖.psd",
"software": "sketch.dmg"
}
而在實際的爬蟲開發過程中,網頁形式遠比以上的復雜。其實遇到最多的問題是在一組列表中嵌套一個列表,我們需要把這種形式提取出來。比如像下面這種形式
{
"a": "a",
"b": [
{"c": "c1", "d": "d1"},
{"c": "c2", "d": "d2"}]
}
他提取出信息后應該是這樣
[
{
"a": "a",
"c": "c1",
"d": "d1"
},
{
"a": "a",
"c": "c2",
"d": "d2"
}
]
如果小伙伴對于算法熟悉的話,應該能察覺出這種遍歷用遞歸來寫是非常方便的。但要注意的是 python 會限定遞歸的層數,小伙伴可以通過下面這個方法查看遞歸限定的層數
import sys
print(sys.getrecursionlimit())
>>>1000
我這邊限定的層數是 1k。對于解析網頁來說完全夠用了,如果哪個人把網頁解析邏輯嵌套了 1000 層,我建議你直接跟老板提放棄這個網頁吧!
再進一步分析
我們已經知道對于通用解析來說,就是通過配置解析規則提取頁面的對應信息。而針對有列表層級的網頁可能還涉及遞歸遍歷問題。那如何去配置這種解析規則呢?其實很簡單,只需要在進入每一個層級之前先指定該層的數據形式,比如下面這個原數據
{
"a": "a",
"b": [
{"c": "c1", "d": "d1"},
{"c": "c2", "d" : "d2"}
]
}
想提取嵌套信息,我們的解析規則就應該是這樣的
[
{
"$name": "a",
"$value_type": "raw",
"$parse_method": "json",
"$parse_rule": "a",
"$each": []
},
{
"$name": "__datas__",
"$value_type": "recursion",
"$parse_method": "json",
"$parse_rule": "b",
"$each": [
{
"$name": "c",
"$value_type": "raw",
"$parse_method": "json",
"$parse_rule": "c",
"$each": []
},
{
"$name": "d",
"$value_type": "raw",
"$parse_method": "json",
"$parse_rule": "d",
"$each": []
}
]
}
]
其中 $name 字段表示我們最終希望最外層數據所擁有的字段名,當然如果是需要遞歸到內層的字段,則將列表保存為 __datas__ ,然后根據這個 __datas__ 進行內層結構的解析。最終我們得到的數據結構應該是這樣的
[
{"a": "a", "c": "c1", "d": "d1"},
{"a": "a", "c": "c2", "d": "d2"}
]
以上我們只演示了 json 的解析規則,如果要拿來解析 html 對象呢?很簡單,將解析方式改為 xpath 對象,然后傳入 xpath 解析語法即可。
代碼實現
總共分成兩部分,一部分根據原最終結果和規則進行打包,將所有涉及 recursion 邏輯的字段進行轉換,代碼如下
def _pack_json(result, rules):
item = {}
for p_rule in rules:
if p_rule.get("$value_type") == "raw":
if p_rule.get("$parse_method") == "json":
item[p_rule.get("$name")] = glom(result, p_rule.get("$parse_rule"))
elif p_rule.get("$value_type") == "recursion":
if p_rule.get("$parse_method") == "json":
tmp_result = glom(result, p_rule.get("$parse_rule"))
total_result = []
for per_r in tmp_result:
total_result.append(_pack_json(per_r, p_rule.get("$each")))
item[p_rule.get("$name")] = total_result
return item
另外一部分將上一步得到的進行解析,將打包得到的結果進行解包,即將所有內嵌的數據提到最外層,代碼如下
def _unpack_datas(result: dict) -> list:
if "__datas__" not in result:
return [result]
item_results = []
all_item = result.pop("__datas__")
for per_item in all_item:
if "__datas__" in per_item:
tmp_datas = per_item.pop("__datas__")
for per_tmp_data in tmp_datas:
tmp_item = _unpack_datas(per_tmp_data)
for per_tmp_item in tmp_item:
item_results.append({**per_tmp_item, **per_item})
else:
item_results.append({**result, **per_item})
return item_results
后再包一層執行入口就可以了,完整代碼如下
from loguru import logger
from glom import glom
def parse(result, rules):
def _pack_json(result, rules):
item = {}
for p_rule in rules:
if p_rule.get("$value_type") == "raw":
if p_rule.get("$parse_method") == "json":
item[p_rule.get("$name")] = glom(result, p_rule.get("$parse_rule"))
elif p_rule.get("$value_type") == "recursion":
if p_rule.get("$parse_method") == "json":
tmp_result = glom(result, p_rule.get("$parse_rule"))
total_result = []
for per_r in tmp_result:
total_result.append(_pack_json(per_r, p_rule.get("$each")))
item[p_rule.get("$name")] = total_result
return item
def _unpack_datas(result: dict) -> list:
if "__datas__" not in result:
return [result]
item_results = []
all_item = result.pop("__datas__")
for per_item in all_item:
if "__datas__" in per_item:
tmp_datas = per_item.pop("__datas__")
for per_tmp_data in tmp_datas:
tmp_item = _unpack_datas(per_tmp_data)
for per_tmp_item in tmp_item:
item_results.append({**per_tmp_item, **per_item})
else:
item_results.append({**result, **per_item})
return item_results
pack_result = _pack_json(result, rules)
logger.info(pack_result)
return _unpack_datas(pack_result)
以上,就是通用解析器的完整案例。案例中僅實現了對于 json 的支持,小伙伴可以基于自己的項目,改造成其他的解析形式。通用解析其實是雞仔為了偷懶寫的,因為雞仔發現,在爬蟲開發中,大部分工作都耗在解析這部分。而有了通用解析的前端頁面,運營和數據分析師就可以根據自己的需要配置自己想爬取的站點了。人生苦短,你懂得。我去摸魚了~
實現方式請移步至 github 查看:https://github.com/hacksman/learn_lab/blob/master/small_bug_lab/general_parser.py
以上就是python 用遞歸實現通用爬蟲解析器的詳細內容,更多關于python 遞歸實現爬蟲解析器的資料請關注腳本之家其它相關文章!
您可能感興趣的文章:- Python爬蟲之爬取2020女團選秀數據
- python爬蟲之教你如何爬取地理數據
- Python爬蟲之教你利用Scrapy爬取圖片
- 基于python分布式爬蟲并解決假死的問題
- python PyQt5 爬蟲實現代碼
- 用python爬蟲爬取CSDN博主信息
- 利用Python網絡爬蟲爬取各大音樂評論的代碼
- Python爬蟲部分開篇概念講解
- python爬蟲之你好,李煥英電影票房數據分析