1. 前言
最近發(fā)現(xiàn)了一個(gè)提供表情包的網(wǎng)址,覺得上面的內(nèi)容不錯(cuò),于是就考慮用Python爬蟲獲取上面表情包的下載鏈接。整體而言,實(shí)現(xiàn)這個(gè)挺簡(jiǎn)單的,就是找到提供表情包json數(shù)據(jù)的api接口即可,接口中沒有任何加密操作。網(wǎng)址為:表情包
2. 具體實(shí)現(xiàn)
還是通過搜索功能,找到匹配搜索詞的相關(guān)表情包,如下:
可以發(fā)現(xiàn),當(dāng)向下滑動(dòng)滾動(dòng)條時(shí),表情包數(shù)據(jù)進(jìn)行動(dòng)態(tài)刷新增加,由此可以判定這個(gè)界面的表情包數(shù)據(jù)是通過請(qǐng)求api接口,然后一些js操作實(shí)現(xiàn)的。直接使用requests模塊訪問當(dāng)前界面,你是無法訪問到這個(gè)表情包數(shù)據(jù)的。實(shí)現(xiàn)上的確是這樣,打開開發(fā)者工具,來到網(wǎng)絡(luò)下的Fetch/XHR,可以找到一個(gè)api接口鏈接,打開這個(gè)接口,那里面有我們想要的表情包數(shù)據(jù)。
這個(gè)接口鏈接為:https://www.dbbqb.com/api/search/json?start=0&w=%E9%BE%99%E7%8E%8B,明顯這個(gè)w后的參數(shù)值應(yīng)該就是我們搜索的詞進(jìn)行的編碼操作。
至于start后的參數(shù)值,應(yīng)該用于分頁的操作,通過向下滑動(dòng)滾動(dòng)條,發(fā)現(xiàn)這個(gè)參數(shù)值初始為0,第二頁為100,第三頁為200,。。。。。。
至于這個(gè)接口中的數(shù)據(jù),的確是圖片的下載鏈接,如下:
3. 實(shí)現(xiàn)代碼
實(shí)現(xiàn)代碼僅僅把這些表情包的下載鏈接獲取到,至于怎樣下載,讀者自行操作。可以考慮使用
from urllib import request
url = 'https://image.dbbqb.com/202308120759/a94be64f09fb4528ed75039698d47321/NEN22'
request.urlretrieve(url=url,filename='龍王.png')
或者
import requests
url = 'https://image.dbbqb.com/202308120759/a94be64f09fb4528ed75039698d47321/NEN22'
rsp = requests.get(url=url)
with open(file='龍王2.png',mode='wb') as f:
f.write(rsp.content)
參考代碼如下:文章來源:http://www.zghlxwxcb.cn/news/detail-645023.html
import requests
import json
from urllib import parse
from crawlers.userAgent import useragent
keyword = input('搜索關(guān)鍵詞:')
pages = input('頁數(shù):')
u = useragent()
# pages
url2 = 'https://image.dbbqb.com/'
encode_kw = parse.quote(keyword)
print(encode_kw)
for i in range(int(pages)):
url = f'https://www.dbbqb.com/api/search/json?start={i*100}&w={encode_kw}'
print(f'第{i + 1}頁->url:{url}')
headers = {
'user-agent':u.getUserAgent(),
"Accept":"application/json",
"Cache-Control":"no-cache",
"Connection":"keep-alive",
"Content-Type":"application/json",
"Cookie":"Hm_lvt_7d2469592a25c577fe82de8e71a5ae60=1690285252,1690367974,1690963288,1691797900; Hm_lpvt_7d2469592a25c577fe82de8e71a5ae60=1691798363",
"sec-ch-ua":";Not A Brand;v=99, Chromium;v=94",
"sec-ch-ua-mobile":"?0",
"sec-ch-ua-platform":"Windows",
"Sec-Fetch-Dest":"empty",
"Sec-Fetch-Mode":"cors",
"Sec-Fetch-Site":"same-origin",
"Web-Agent":"web",
}
rsp = requests.get(url=url,headers=headers)
arr = json.loads(rsp.text)
for e in arr:
download_url = url2 + e['path']
print(download_url)
運(yùn)行結(jié)果:
上述內(nèi)容僅學(xué)習(xí)使用,不能用于商業(yè)活動(dòng),希望讀者切記。文章來源地址http://www.zghlxwxcb.cn/news/detail-645023.html
到了這里,關(guān)于Python爬蟲:抓取表情包的下載鏈接的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!