用Python爬虫爬取王者荣耀官网图片
时间: 2024-05-24 21:05:08 浏览: 12
好的,以下是使用Python爬虫爬取王者荣耀官网图片的步骤:
1. 导入需要的库,包括`requests`用于发送HTTP请求,`bs4`用于解析HTML页面。
```
import requests
from bs4 import BeautifulSoup
```
2. 发送HTTP请求获取HTML页面,并使用`BeautifulSoup`解析页面。
```
url = 'https://pvp.qq.com/web201605/herolist.shtml'
html = requests.get(url).content.decode('gbk')
soup = BeautifulSoup(html, 'html.parser')
```
3. 找到图片所在的HTML标签,并提取图片的URL。
```
img_tags = soup.find_all('img', class_='pic-img')
img_urls = [tag['src'] for tag in img_tags]
```
4. 遍历图片URL列表,发送HTTP请求并保存图片。
```
for i, url in enumerate(img_urls):
response = requests.get(url)
with open(f'{i}.jpg', 'wb') as f:
f.write(response.content)
```
这样就可以爬取王者荣耀官网上的所有英雄头像图片并保存到本地。
相关问题
Python爬虫爬取王者荣耀官网图片
好的,为了爬取王者荣耀官网的图片,我们需要用到Python中的requests和beautifulsoup4库来获取网页源代码以及解析网页内容。具体的步骤如下:
1. 导入requests和beautifulsoup4库。
```python
import requests
from bs4 import BeautifulSoup
```
2. 使用requests库获取网页源代码。
```python
url = "https://pvp.qq.com/web201605/herolist.shtml"
response = requests.get(url)
html = response.content.decode("gbk")
```
3. 使用beautifulsoup4库解析网页内容,获取图片链接。
```python
soup = BeautifulSoup(html, "html.parser")
hero_list = soup.select(".herolist > li")
for hero in hero_list:
hero_name = hero.find("img")["alt"]
hero_img = hero.find("img")["src"]
img_url = "https:" + hero_img
```
4. 使用requests库下载图片。
```python
img_response = requests.get(img_url)
with open(hero_name + ".jpg", "wb") as f:
f.write(img_response.content)
```
以上就是爬取王者荣耀官网图片的基本步骤,具体实现可以根据自己的需求进行改进。
爬虫爬取王者荣耀皮肤
爬取王者荣耀皮肤可以使用Python的爬虫技术。以下是一个简单的示例代码,演示如何使用Python爬虫爬取王者荣耀皮肤图片:
```python
import requests
import os
def download_image(url, save_path):
response = requests.get(url)
with open(save_path, 'wb') as f:
f.write(response.content)
def crawl_hero_skins(hero_id):
url = f'https://pvp.qq.com/web201605/js/herolist.json'
response = requests.get(url)
hero_list = response.json()
for hero in hero_list:
if hero['ename'] == hero_id:
hero_name = hero['cname']
skin_list = hero['skin_name'].split('|')
for index, skin_name in enumerate(skin_list):
skin_url = f'https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{hero_id}/{hero_id}-bigskin-{index+1}.jpg'
save_path = f'{hero_name}/{skin_name}.jpg'
os.makedirs(os.path.dirname(save_path), exist_ok=True)
download_image(skin_url, save_path)
print(f'Downloaded {skin_name}.jpg')
crawl_hero_skins(105) # 以英雄ID 105(貂蝉)为例进行爬取
```
这段代码会爬取王者荣耀中英雄ID为105(貂蝉)的所有皮肤图片,并保存到以英雄名字命名的文件夹中。你可以根据需要修改代码中的英雄ID来爬取其他英雄的皮肤图片。
注意:爬取网站内容时,请遵守相关网站的使用规则和法律法规。