用Python3Request爬取英雄联盟皮肤、单线程爬取
生活随笔
收集整理的這篇文章主要介紹了
用Python3Request爬取英雄联盟皮肤、单线程爬取
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
"""
Hero_LOL 和王者榮耀類似
"""
import requests
import re
import json
import os
import threadingdef hero(hero_name, hero_num):#統一前戳h_l = "https://ossweb-img.qq.com/images/lol/web201310/skin/big"# 逐一遍歷英雄print(hero_num.__len__())num = 0 # 為了獲取英雄的號碼for i in hero_num:# 逐一遍歷皮膚,此處假定一個英雄最多15個皮膚for sk_num in range(0, 15): # 從第0個皮膚開始hsl = h_l + i + "00" + str(sk_num) + ".jpg"hl = requests.get(hsl)if hl.status_code == 200:filename = "LOL/" + str(hero_name[num]) + str(sk_num) + ".jpg"print("此時正在下載:" + filename+" 這是第"+str(num+1)+"個英雄")with open(filename, "wb") as f:f.write(hl.content)else:breaknum += 1def main():"""#獲取全部英雄數據:return:"""#JS_urlHero_url = "https://lol.qq.com/biz/hero/champion.js"#User-Agent偽裝瀏覽器標識header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'}#Get source code(獲取源代碼)response = requests.get(Hero_url,headers = header)#decode 編碼為GBK,方便下方使用正則遍歷response = response.content.decode("GBK")#regex(正則表達式)reg = """keys":([\s\S]*?),"data"""#預編譯regex = re.compile(reg,re.IGNORECASE) #第一個參數 正則表達式 第二個參數忽略的意思,忽略大小寫#findall 就是獲取response內所有符合規則的數據,返回值為listresponse_s = regex.findall(response)#也就是所有符合規則的數據放到了列表里,如果只有一個,那就是list[0]response_s = response_s[0]#通過eval轉換為一個dict,具體功能自己查(其實我也沒大懂)res = eval(response_s)#get the keys in the dict(再轉換為list)hero_num = list(res.keys()) #Hero_number#get the values in the dict (再轉換為list)hero_name = list(res.values())#Hero_name
hero(hero_name,hero_num)if __name__ == '__main__':main()
?
轉載于:https://www.cnblogs.com/wxzbk/p/10983976.html
總結
以上是生活随笔為你收集整理的用Python3Request爬取英雄联盟皮肤、单线程爬取的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LruCache缓存bitmap(一)
- 下一篇: 网络安全技术文章征稿启事