前言:
大眾點(diǎn)評(píng)是一款非常受大眾喜愛的第三方的美食相關(guān)的點(diǎn)評(píng)網(wǎng)站
因此,該網(wǎng)站的數(shù)據(jù)也就非常具有價(jià)值,優(yōu)惠,評(píng)價(jià)數(shù)量,好評(píng)度等數(shù)據(jù)也就非常受數(shù)據(jù)公司的歡迎。
今天就寫一個(gè)簡單的大眾點(diǎn)評(píng)列表頁數(shù)據(jù)抓取
頁面解析:

從圖中可以看到對(duì)應(yīng)的評(píng)價(jià)數(shù)字都是方框,所以對(duì)這些要進(jìn)行一下分析
先做一個(gè)簡單的采集處理,查看一下控制臺(tái)輸出的內(nèi)容,以及直接在網(wǎng)頁中查看源代碼


可以看到一下兩種內(nèi)容
{'店鋪': '夏氏砂鍋(萬松園店)', '評(píng)分': '4.79', '評(píng)價(jià)數(shù)': '1\ueeed\uf119\uf7c4\uf40d', '人均消費(fèi)': '¥\uf796\uf796', '菜系': '\ue39f\ue7d0\ue307 \ue64e\uf4af\uf3cd', '地址': '雪\uf861\ueadb73\uf1b3', '口味': '\ueeed.\uf7961', '環(huán)境': '\ueeed.\ue7dd\uf2d4', '服務(wù)': '\ueeed.\ue411\uf7c4'}
<b>1<svgmtsi class="shopNum"></svgmtsi><svgmtsi class="shopNum"></svgmtsi><svgmtsi class="shopNum"></svgmtsi></b>
<b>¥<svgmtsi class="shopNum"></svgmtsi><svgmtsi class="shopNum"></svgmtsi></b>
可以看出,評(píng)價(jià)數(shù)及人均消費(fèi)價(jià)格被隱藏。
這種方式被稱為svg映射。
接下來我們就要搞定這種類型的數(shù)據(jù)。
字體解析:
首先我們需要找到網(wǎng)頁打開后,他們所引用的woff字體文件
在F12中進(jìn)行抓包,選中Network,然后再次選中菜單欄中的Font?,F(xiàn)在,在雜亂的網(wǎng)絡(luò)訪問內(nèi)容中,就只有兩個(gè)woff文件了。
下一步就是打開這個(gè)字體文件:
- 下載好目標(biāo)woff文件,直接右鍵訪問內(nèi)容 ,選中"Open in new tab"就會(huì)下載這個(gè)woff文件。
- 打開https://www.fontke.com/tool/convfont這個(gè)地址,在線轉(zhuǎn)換成ttf文件。
- 再打開http://fontstore.baidu.com/static/editor/index.html這個(gè)地址,在線解析剛打開的ttf文件。
-
會(huì)看到以下內(nèi)容

- 查看字形,進(jìn)行分析
- 通過觀察,我們看到在百度字體解析的文字代碼中,都是以“unie”開頭的,那么我們將之前看到的編碼內(nèi)容組合后進(jìn)入字體文件中搜索,就可以驗(yàn)證該編碼是否正確對(duì)應(yīng)網(wǎng)頁顯示的數(shù)字了
構(gòu)造映射字典
從剛才的頁面解析中,我們已經(jīng)明白了“\uf119”是0,“\uf796”是7,那么咱們再次在頁面中找到其他的相關(guān)數(shù)字,【0-9】十個(gè)數(shù)字很容易就組合出來了。記住,1不需要重構(gòu)
self.woff = {
"\uf119": "0",
# "": "1",
"\uf40d": "2",
"\uf2d4": "3",
"\ueeed": "4",
"\ue411": "5",
"\ue7dd": "6",
"\uf7c4": "7",
"\uf796": "8",
"\ue41e": "9",
}
注意:字體svg經(jīng)常改變,需要自己對(duì)應(yīng)當(dāng)時(shí)的情況寫一下
解析處理數(shù)據(jù)
import requests
from pyquery import PyQuery as pq
def get_session():
req = requests.Session()
#添加請(qǐng)求頭
req.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"
}
return req
def get_num(uncode_list):
#構(gòu)造字體映射字典
woff = {
"\uf119": "0",
# "": "1",
"\uf40d": "2",
"\uf2d4": "3",
"\ueeed": "4",
"\ue411": "5",
"\ue7dd": "6",
"\uf7c4": "7",
"\uf796": "8",
"\ue41e": "9",
}
count = ""
for uncs in uncode_list:
if uncs in woff.keys():
cc = woff[uncs]
else:
cc = uncs
count += cc
return count
def get_html(res):
info = {}
#pyquery 解析
content = pq(res.text)
content = content("div #shop-all-list ul li").items()
for doc in content:
info["店鋪"] = doc("div .tit a h4").text()
info["評(píng)分"] = doc("div .nebula_star ").text()
review_num = doc("div.comment a.review-num b").text().replace("\n","")
info["評(píng)價(jià)數(shù)"] = get_num(review_num)
price = doc("div.comment a.mean-price b").text().replace("\n","")
info["人均消費(fèi)"] = get_num(price)
#info["菜系"] = doc("div.tag-addr span.tag").text().replace("\n","")
#info["地址"] = doc("span.addr").text().replace("\n","")
flavor = doc("span.comment-list span:nth-child(1) b").text().replace("\n","")
info["口味"] = get_num(flavor)
environment = doc("span.comment-list span:nth-child(2) b").text().replace("\n", "")
info["環(huán)境"] = get_num(environment)
service = doc("span.comment-list span:nth-child(3) b").text().replace("\n", "")
info["服務(wù)"] = get_num(service)
print(info)
if __name__ == '__main__':
url = "http://www.dianping.com/wuhan/ch10"
req = get_session()
res = req.get(url)
get_html(res)
解析結(jié)果
