Python 爬取王者荣耀英雄和皮肤图片并生成词云
import requests from bs4 import BeautifulSoup import lxml import sqlite3 import jieba from wordcloud import WordCloud import openpyxl
爬取英雄和皮肤图片的网站
url = 'https://pvp.qq.com/web201605/herolist.shtml'
发送请求获取网页内容
response = requests.get(url) html = response.content
使用BeautifulSoup解析网页
soup = BeautifulSoup(html, 'lxml')
获取所有英雄的图片链接
hero_imgs = [] for img in soup.find_all('img', class_='pic-img'): hero_imgs.append(img['src'])
获取所有皮肤的图片链接
skin_imgs = [] for img in soup.find_all('img', class_='skin-img'): skin_imgs.append(img['src'])
将信息保存到Excel表中
wb = openpyxl.Workbook() ws1 = wb.active ws1.title = 'Heroes' ws1.append(['英雄图片链接']) for img in hero_imgs: ws1.append([img])
ws2 = wb.create_sheet('Skins') ws2.append(['皮肤图片链接']) for img in skin_imgs: ws2.append([img])
wb.save('hero_skin_images.xlsx')
生成英雄名称的词云图
hero_names = [] for name in soup.find_all('a', class_='herolist-content__name'): hero_names.append(name.text)
cut_text = ' '.join(jieba.cut(''.join(hero_names))) wordcloud = WordCloud(font_path='msyh.ttc', background_color='white').generate(cut_text) wordcloud.to_file('hero_names_wordcloud.png')
原文地址: https://www.cveoy.top/t/topic/oQmU 著作权归作者所有。请勿转载和采集!