爬取王者荣耀英雄信息 - 英雄编号、英雄名称、皮肤名称和皮肤图片链接,并存入桌面上的csv
时间: 2024-06-09 21:10:41 浏览: 25
好的,这里提供一个Python爬取王者荣耀英雄信息的示例代码,你可以参考一下:
```python
import requests
import csv
from bs4 import BeautifulSoup
# 设置请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0;Win64) AppleWebkit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 发送请求
url = 'https://pvp.qq.com/web201605/herolist.shtml'
response = requests.get(url, headers=headers)
response.encoding = response.apparent_encoding
# 解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
hero_list = soup.find('ul', class_='herolist clearfix')
# 创建CSV文件
csv_file = open('C:/Users/XXX/Desktop/hero_info.csv', 'w', newline='', encoding='utf-8')
writer = csv.writer(csv_file)
writer.writerow(['编号', '英雄名称', '皮肤名称', '皮肤图片链接'])
# 遍历英雄列表,获取英雄信息
for hero in hero_list.find_all('li'):
hero_id = hero.find('i').text
hero_name = hero.find('img')['alt']
skins = hero.find_all('a')
for skin in skins:
skin_name = skin.text
skin_url = 'https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' + str(hero_id) + '/' + str(
hero_id) + '-bigskin-' + skin['href'].split('.')[-2].split('-')[-1] + '.jpg'
writer.writerow([hero_id, hero_name, skin_name, skin_url])
csv_file.close()
print('英雄信息已保存至桌面hero_info.csv文件中!')
```
这个代码会将英雄编号、英雄名称、皮肤名称和皮肤图片链接保存到桌面上的hero_info.csv文件中。你可以根据自己的需求修改文件路径和文件名。注意,这个代码需要安装requests和beautifulsoup4库才能运行。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)