python爬取半次元圖片源碼,由大神自制的python爬取工具,本源碼針對半次元圖片平臺,可以爬取最新的網(wǎng)站圖片資源,支持自定義保存目錄,非常方便,需要requests庫的支持,想要相關(guān)源碼資源的朋友們不要錯過哦!
python爬取半次元圖片源碼說明:
需要安裝requests庫,在運行腳本的文件夾下新建一個img文件夾
源碼也可供大家學(xué)習(xí)和參考。
python爬取半次元圖片源碼一覽:
import requests
import re
url = 'https://bcy.net/coser/toppost100' # 要進(jìn)行抓取的url
web_url = "https://bcy.net" # 官方網(wǎng)站
file = 'img/' # 文件的保存路徑最后加反斜杠
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
}
# 獲取抓取數(shù)據(jù)頁面
data = requests.get(url, headers=headers).text
wi_url_id = re.findall('<a href=".*?class="db posr ovf"', data)
# 對抓取圖片單獨頁面url進(jìn)行遍歷
for s in wi_url_id:
wi_id = web_url + s.lstrip('<a href="').rstrip('" class="db posr ovf"')
n_data = requests.get(wi_id, headers=headers).text # 獲取單獨的圖片頁面數(shù)據(jù)
json_data = re.findall('"{.*?}"', n_data)[0].lstrip('"').rstrip('}}"')
n_http = re.findall('"path.*?w650', json_data)
# 對圖片url進(jìn)行遍歷
for b in n_http:
try:
img_data = b.lstrip('"path\\":\\"s') + '.image'
img_url = 'https:/' + img_data.replace('u002F', '').replace('\\\\', '/')
img = requests.get(img_url, headers=headers).content # 獲取圖片數(shù)據(jù)
img_name = img_url.rstrip('.jpg~tplv-banciyuan-w650.image')[-31:] # 獲取圖片名
# 對圖片進(jìn)行保存
with open(file + img_name + '.jpg', 'wb') as f:
f.write(img)
print('以保存,圖片url:' + img_url)
except:
print('保存失敗')