本帖最后由 kay2kay 于 2021-06-01 22:02 编辑
import requests import os from bs4 import BeautifulSoup hero_url = 'https://lol.garena.tw/' url = 'https://lol.garena.tw/champions' res = requests.get(url) soup = BeautifulSoup(res.text, 'lxml') api_url = 'https://mock.mengxuegu.com/mock/608b9c271d10f86a7bd6aa6c/garena_text/Hero_list' hero = requests.get(api_url).json() hero_len = len(hero['Hero_list']) # 155个 box = soup.find_all("a", class_="box") for i in range(0, hero_len): # print(hero['Hero_list'][i]['ENM']) # 根据汉字名创建文件夹 cnm = hero['Hero_list'][i]['CNM'] os.mkdir(cnm) hero_short_url = box[i].attrs['href'] complete_url = hero_url + hero_short_url # print(complete_url) res_2 = requests.get(complete_url) soup_2 = BeautifulSoup(res_2.text, 'lxml') swiper_wrapper = soup_2.find_all("div", class_='swiper-slide') skin_num = int(len(swiper_wrapper) / 2) # 共计多少个皮肤 skin_total = swiper_wrapper[-skin_num:] for w in range(0, skin_num): print(skin_total[w].text) skin_title = str(skin_total[w].text).replace('/', '') skin_img_src = skin_total[w].find('img').get('src') print(skin_img_src) tui_res = requests.get(skin_img_src) # './image/img3.png' filename = './' + cnm + '/' + skin_title + '.jpg' with open(filename, "wb") as f: f.write(tui_res.content)
加入此段代码skin_title = str(skin_total[w].text).replace('/', '') 是因为有K/DA皮肤,在路径保存的时候,会扰乱本地保存路径 本来是想转义字符串,可是尝试了好多次,没有成功, 如有更好的方法,请大家多多指教
|