網上關于爬蟲大部分教程和編輯器用的都不是vscode ,此教程用到了vscode、Python、bs4、requests。
vscode配置Python安裝環境可以看看這個大佬的教程?03-vscode安裝和配置_嗶哩嗶哩_bilibili
vscode配置爬蟲環境可以參考這個大佬的教程【用Vscode實現簡單的python爬蟲】從安裝到配置環境變量到簡單爬蟲以及python中pip和request,bs4安裝_vscode爬蟲-CSDN博客
爬蟲代碼如下
#按照指令升級pip庫,如果無法解析pip指令說明系統變量環境path中缺少了Python的路徑,解決辦法:https://zhuanlan.zhihu.com/p/655640807
#發送請求的模塊 pip install requests
import requests
#解析HTML的模塊 pip install bs4
from bs4 import BeautifulSoup
import os
import re headers1={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"}def requests_url(req_url):response= requests.get(req_url,headers=headers1)response.encoding='gbk' #網頁編碼gbkreturn response.text#獲取英雄列表里面的英雄詳情頁網址以及英雄編號
#https://pvp.qq.com/web201605/herolist.shtml
#解析標簽,獲取到英雄詳情頁以及英雄名字<a href="herodetail/lianpo.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/105/105.jpg" width="91" height="91" alt="廉頗">廉頗</a>
#
herolist_resp= requests_url("https://pvp.qq.com/web201605/herolist.shtml")
soup = BeautifulSoup(herolist_resp,"html.parser")
ul = soup.find_all("ul",attrs={"class":"herolist clearfix"})
icon_list = ul[0].find_all("a")for i,n in enumerate(icon_list):hrefs=n.get("href") url = "https://pvp.qq.com/web201605/"+ hrefsid = re.findall(r'\d+',hrefs)[0] #獲取英雄編號imgs=n.findAll('img')[0]c_name= imgs.get("alt")local_path = "王者榮耀\\"+c_name+"\\" #創建英雄文件夾if not os.path.exists(local_path):os.makedirs(local_path)#獲取詳情頁herodetail_resp = requests_url(url)soup = BeautifulSoup(herodetail_resp,"html.parser")ul = soup.findAll("ul",attrs={"class":"pic-pf-list pic-pf-list3"})#data-imgname屬性獲取names = ul[0].get("data-imgname")names=[name[0:name.index('&')]for name in names.split('|')]print(names)#提取皮膚名字for i,n in enumerate(names) :print (n)# for num in range(105,108): #563#response = requests.get(f"https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{num}/{num}-bigskin-1.jpg",headers=headers1)response = requests.get(f"https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{id}/{id}-bigskin-{i+1}.jpg",headers=headers1)#保存圖片with open (local_path+f"{n}.jpg",'wb') as f:f.write(response.content)
此爬蟲支持不同英雄的壁紙根據皮膚名稱分類存放,具體效果可以觀看B站視頻vscode編寫Python爬蟲,爬取王者榮耀皮膚壁紙_嗶哩嗶哩_bilibili。