好友
阅读权限10
听众
最后登录1970-1-1
|
苹果刺客
发表于 2021-6-1 10:42
本帖最后由 苹果刺客 于 2021-6-1 12:59 编辑
[Python] 纯文本查看 复制代码 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皮肤,在路径保存的时候,会扰乱本地保存路径
本来是想转义字符串,可是尝试了好多次,没有成功,
如有更好的方法,请大家多多指教 |
免费评分
-
查看全部评分
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|