前言
本文的文字及圖片來源於網絡,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯繫我們以作處理。
PS:如有需要Python學習資料的小夥伴可以加點擊下方鏈接自行獲取http://t.cn/A6Zvjdun
開發工具
- python版本:3.6.8
- 編輯器:pycharm
相關模塊
import requests
import re
import json
import os
模塊安裝
pip install -i https://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com requests
運行效果
完整代碼
import requests
import re
import json
import os
def getHtml(url):
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding
except:
print(url + "爬取失敗!")
else:
response = r.text
getInfo(response)
def getInfo(res):
lists = re.findall(r'"keys":(.*?),"data"', res)
# print(lists)
hero_id = json.loads(lists[0])
# print(hero_id)
for hero in hero_id.values():
getSkin(hero)
def getSkin(hero):
url = 'https://lol.qq.com/biz/hero/' + hero + '.js'
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding
except:
print(url + "爬取失敗!")
else:
html = r.text
num = re.findall(r'"id":"(\d{4,6})","num"', html)
for i in range(len(num)):
img_url = 'https://game.gtimg.cn/images/lol/act/img/skin/big' + num[i] + '.jpg'
save_img(hero, img_url)
def save_img(hero, img_url):
root = hero + "\\"
path = root + img_url.split('/')[-1]
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r = requests.get(img_url)
with open(path, 'wb') as f:
f.write(r.content)
f.close()
print("文件保存成功!")
else:
print("文件已存在!")
except:
print("爬取失敗!")
print(img_url + "已下載")
def main():
url = "https://lol.qq.com/biz/hero/champion.js"
getHtml(url)
if __name__ == "__main__":
main()
提莫隊長正在待(song)命~