爬取王者荣耀英雄信息 - 英雄编号、英雄名称、皮肤名称和皮肤图片链接,并存到桌面上
时间: 2024-06-09 20:10:40 浏览: 18
好的,这里给出一个简单的Python爬虫示例代码,使用requests和BeautifulSoup库:
```python
import os
import requests
from bs4 import BeautifulSoup
# 创建桌面目录下的hero_images文件夹用于存储皮肤图片
desktop_path = os.path.join(os.path.expanduser("~"), 'Desktop')
image_folder = os.path.join(desktop_path, 'hero_images')
if not os.path.exists(image_folder):
os.mkdir(image_folder)
# 获取英雄列表页面
url = 'https://pvp.qq.com/web201605/herolist.shtml'
resp = requests.get(url)
resp.encoding = 'gbk' # 网页使用gbk编码
# 解析页面,获取英雄信息
soup = BeautifulSoup(resp.text, 'html.parser')
hero_list = soup.find_all('a', class_='herolist-content__pic')
for hero in hero_list:
hero_name = hero.find('img')['alt']
hero_id = hero['href'].split('/')[-1].split('.')[0]
# 获取英雄皮肤列表页面
skin_url = f"https://pvp.qq.com/web201605/herodetail/{hero_id}.shtml"
skin_resp = requests.get(skin_url)
skin_resp.encoding = 'gbk'
# 解析页面,获取皮肤信息
skin_soup = BeautifulSoup(skin_resp.text, 'html.parser')
skin_list = skin_soup.find_all('li', class_='pic-pf')
for skin in skin_list:
skin_name = skin.find('img')['alt']
skin_img_url = skin.find('img')['src']
# 下载皮肤图片
img_resp = requests.get(skin_img_url)
img_name = f"{hero_name}_{skin_name}.jpg"
img_path = os.path.join(image_folder, img_name)
with open(img_path, 'wb') as f:
f.write(img_resp.content)
# 输出英雄和皮肤信息
print(f"英雄编号:{hero_id},英雄名称:{hero_name},皮肤名称:{skin_name},皮肤图片链接:{skin_img_url},已保存到:{img_path}")
```
运行以上代码后,即可在桌面上的hero_images文件夹中看到下载的皮肤图片,并在终端输出英雄和皮肤信息。