I. Introduction
A few days ago, someone asked me to help him download the hero poster of the King of Glory, so I thought about writing a program in Python, otherwise it would be too troublesome to download one by one, and it is not our style
2. Library installation
pip install beautifulsoup4
pip install lxml
3. Edit the code
import requests, re
from bs4 import BeautifulSoup
url = "https://pvp.qq.com/web201605/herolist.shtml"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.203"}
html = requests.request("get", url=url, headers=headers).text
soup = BeautifulSoup(html, "lxml")
list = []
for i in soup.find_all(attrs={"class": "herolist clearfix"}):
for o in i.find_all("a"):
list.append(f"https://pvp.qq.com/web201605/" + o.get("href"))
count = 1
for ix in list:
htm = requests.get(url=ix, headers=headers)
htm.encoding = "GBK"
soup1 = BeautifulSoup(htm.text, "lxml")
for z in soup1.find_all(attrs={"class": "zk-con1"}):
name = soup1.find(attrs={"class": "cover-name"}).get_text()
# print(z)
ZG = z.get("style")
img_urls = re.search("//.*?.jpg", str(ZG))
imgs = f"http:" + img_urls.group()
pic = requests.get(imgs).content
with open(f"{name}.jpg", "wb") as f:
f.write(pic)
print(f"正在下载 === {name} == 英雄图片,这是第{count}张图片")
count += 1
print("图片下载完毕!")
4. Effect