{ "title": "Python爬取王者荣耀皮肤图片并生成词云图", "description": "使用Python编写一个程序,爬取王者荣耀官网皮肤图片,并保存到SQLite数据库或Excel表格中,最后生成英雄名称词云图。", "keywords": "Python, 爬虫, 王者荣耀, 皮肤图片, 词云图, SQLite, Excel, BeautifulSoup, requests", "content": "# 导入需要用到的第三方库 import requests from bs4 import BeautifulSoup import sqlite3 import openpyxl from wordcloud import WordCloud import matplotlib.pyplot as plt

定义一个爬取皮肤图片的函数

def get_skin_images(): url = 'https://pvp.qq.com/web201605/herolist.shtml' res = requests.get(url) res.encoding = 'gbk' soup = BeautifulSoup(res.text, 'html.parser') hero_list = soup.find_all('a', {'class': 'herolist'})
for hero in hero_list: hero_name = hero.find('img')['alt'] skin_list_url = 'https://pvp.qq.com/web201605/' + hero['href'] skin_res = requests.get(skin_list_url) skin_res.encoding = 'gbk' skin_soup = BeautifulSoup(skin_res.text, 'html.parser') skin_list = skin_soup.find_all('a', {'class': 'pic-pf'}) for skin in skin_list: skin_name = skin.find('img')['alt'] skin_image_url = 'https:' + skin.find('img')['src'] skin_image_res = requests.get(skin_image_url) with open(hero_name + '_' + skin_name + '.jpg', 'wb') as f: f.write(skin_image_res.content)

定义一个保存信息到sqlite数据库表中的函数

def save_to_sqlite(): conn = sqlite3.connect('data.db') c = conn.cursor() c.execute('''CREATE TABLE IF NOT EXISTS hero_skin (hero_name TEXT, skin_name TEXT, skin_image BLOB)''') url = 'https://pvp.qq.com/web201605/herolist.shtml' res = requests.get(url) res.encoding = 'gbk' soup = BeautifulSoup(res.text, 'html.parser') hero_list = soup.find_all('a', {'class': 'herolist'}) for hero in hero_list: hero_name = hero.find('img')['alt'] skin_list_url = 'https://pvp.qq.com/web201605/' + hero['href'] skin_res = requests.get(skin_list_url) skin_res.encoding = 'gbk' skin_soup = BeautifulSoup(skin_res.text, 'html.parser') skin_list = skin_soup.find_all('a', {'class': 'pic-pf'}) for skin in skin_list: skin_name = skin.find('img')['alt'] skin_image_url = 'https:' + skin.find('img')['src'] skin_image_res = requests.get(skin_image_url) c.execute("INSERT INTO hero_skin (hero_name, skin_name, skin_image) VALUES (?, ?, ?)", (hero_name, skin_name, sqlite3.Binary(skin_image_res.content))) conn.commit() conn.close()

定义一个保存信息到Excel表中的函数

def save_to_excel(): wb = openpyxl.Workbook() sheet = wb.active sheet['A1'] = '英雄名称' sheet['B1'] = '皮肤名称' sheet['C1'] = '皮肤图片' url = 'https://pvp.qq.com/web201605/herolist.shtml' res = requests.get(url) res.encoding = 'gbk' soup = BeautifulSoup(res.text, 'html.parser') hero_list = soup.find_all('a', {'class': 'herolist'}) row = 2 for hero in hero_list: hero_name = hero.find('img')['alt'] skin_list_url = 'https://pvp.qq.com/web201605/' + hero['href'] skin_res = requests.get(skin_list_url) skin_res.encoding = 'gbk' skin_soup = BeautifulSoup(skin_res.text, 'html.parser') skin_list = skin_soup.find_all('a', {'class': 'pic-pf'}) for skin in skin_list: skin_name = skin.find('img')['alt'] skin_image_url = 'https:' + skin.find('img')['src'] skin_image_res = requests.get(skin_image_url) sheet.cell(row=row, column=1, value=hero_name) sheet.cell(row=row, column=2, value=skin_name) sheet.cell(row=row, column=3).value = openpyxl.drawing.image.Image(io.BytesIO(skin_image_res.content)) row += 1 wb.save('data.xlsx')

定义一个生成词云图的函数

def generate_wordcloud(): url = 'https://pvp.qq.com/web201605/herolist.shtml' res = requests.get(url) res.encoding = 'gbk' soup = BeautifulSoup(res.text, 'html.parser') hero_list = soup.find_all('a', {'class': 'herolist'}) text = '' for hero in hero_list: hero_name = hero.find('img')['alt'] text += hero_name + ' ' wordcloud = WordCloud(background_color='white').generate(text) plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') plt.show()

主程序

if name == 'main': get_skin_images() save_to_sqlite() save_to_excel() generate_wordcloud()

Python爬取王者荣耀皮肤图片并生成词云图

原文地址: https://www.cveoy.top/t/topic/oQkU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录