今天使用python寫了一個簡單的爬蟲,用來下載taptap網(wǎng)站的游戲截圖。下面說下具體的實現(xiàn)方法。
在搜索框中搜索“原神”
打開瀏覽器的開發(fā)者選項,從詳情按鈕里面跳轉(zhuǎn)到游戲的頁面,點擊詳情之后,跳轉(zhuǎn)頁面
這時候看到,瀏覽器地址:https://www.taptap.com/app/168332,tap官網(wǎng)的域名加上app/和圖一的游戲id,可以使用re模塊正則表達(dá)式模塊從接口中獲取到這個id
x_ua = 'V=1PN=WebAppLANG=zh_CNVN_CODE=4VN=0.1.0LOC=CNPLT=PCDS=AndroidUID=97bb961f-bf03-4c7a-8cd7-8d6d8655d9c8DT=PC'
def search(game_name):
"""根據(jù)游戲名搜索提取出游戲id"""
url = 'https://www.taptap.com/webapiv2/mix-search/v1/by-keyword'
data = {
'kw': f'{game_name}',
'X-UA' : x_ua
# 'X-UA': 'V=1PN=WebAppLANG=zh_CNVN_CODE=4VN=0.1.0LOC=CNPLT=PCDS=AndroidUID=97bb961f-bf03-4c7a-8cd7-8d6d8655d9c8DT=PC'
}
r = requests.get(url=url, params=data)
pattern = re.compile('"type":"app","identification":"app:(.+?)"', re.S)
r1 = pattern.findall(r.text)
#從response中提取游戲id
return r1[0]
然后打開開發(fā)者選項觀察接口,可以從這個接口中獲取到游戲截圖的鏈接,
把數(shù)據(jù)json格式化一下,可以發(fā)現(xiàn)在data下,"screenshots"下的“orignal_url”就是截圖的鏈接。使用json.load轉(zhuǎn)換成字典,就可以比較方便的取出內(nèi)容了。
url = f'https://www.taptap.com/webapiv2/app/v2/detail-by-id/{search(game_name)}'
r = requests.get(url=url, params={'X-UA': x_ua})
data = json.loads(r.text)
#轉(zhuǎn)換為字典格式
original_url = data.get('data').get('screenshots')
#提取出截圖url
之后使用一個循環(huán)將圖片保存到本地
for i in original_url:
time.sleep(0.5)
game_original_url = i.get('original_url')
print(game_original_url, type(game_original_url))
respones = requests.get(game_original_url)
img = respones.content
file_name = game_original_url[-36: -4]
screenshots = os.path.join(os.path.dirname(__file__), f'screenshots/{game_name}')
if not os.path.exists(screenshots):
os.makedirs(screenshots)
file_path = os.path.join(screenshots, file_name)
with open(file_path + '.png', 'wb') as f:
f.write(img)
完整代碼:
import os
import requests
import json
import re
import time
x_ua = 'V=1PN=WebAppLANG=zh_CNVN_CODE=4VN=0.1.0LOC=CNPLT=PCDS=AndroidUID=97bb961f-bf03-4c7a-8cd7-8d6d8655d9c8DT=PC'
def search(game_name):
"""根據(jù)游戲名搜索提取出游戲id"""
url = 'https://www.taptap.com/webapiv2/mix-search/v1/by-keyword'
data = {
'kw': f'{game_name}',
'X-UA' : x_ua
# 'X-UA': 'V=1PN=WebAppLANG=zh_CNVN_CODE=4VN=0.1.0LOC=CNPLT=PCDS=AndroidUID=97bb961f-bf03-4c7a-8cd7-8d6d8655d9c8DT=PC'
}
r = requests.get(url=url, params=data)
pattern = re.compile('"type":"app","identification":"app:(.+?)"', re.S)
r1 = pattern.findall(r.text)
#從response中提取游戲id
return r1[0]
def download_screenshots(game_name):
url = f'https://www.taptap.com/webapiv2/app/v2/detail-by-id/{search(game_name)}'
r = requests.get(url=url, params={'X-UA': x_ua})
data = json.loads(r.text)
#轉(zhuǎn)換為字典格式
original_url = data.get('data').get('screenshots')
#提取出截圖url
try:
for i in original_url:
time.sleep(0.5)
game_original_url = i.get('original_url')
print(game_original_url, type(game_original_url))
respones = requests.get(game_original_url)
img = respones.content
file_name = game_original_url[-36: -4]
screenshots = os.path.join(os.path.dirname(__file__), f'screenshots/{game_name}')
if not os.path.exists(screenshots):
os.makedirs(screenshots)
file_path = os.path.join(screenshots, file_name)
with open(file_path + '.png', 'wb') as f:
f.write(img)
except:
print('下載失敗')
if __name__ == '__main__':
download_screenshots('lol')
以上就是使用python爬取taptap游戲截圖的步驟的詳細(xì)內(nèi)容,更多關(guān)于使用python爬取taptap游戲截圖的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:- python爬取天氣數(shù)據(jù)的實例詳解
- 用python爬取歷史天氣數(shù)據(jù)的方法示例
- python爬取哈爾濱天氣信息
- python3爬取各類天氣信息
- Python爬取國外天氣預(yù)報網(wǎng)站的方法
- Python基于Tkinter開發(fā)一個爬取B站直播彈幕的工具
- Python爬蟲之爬取最新更新的小說網(wǎng)站
- Python爬蟲之爬取二手房信息
- Python爬蟲之獲取心知天氣API實時天氣數(shù)據(jù)并彈窗提醒