Python 爬取王者荣耀皮肤图片并保存至数据库和Excel
Python 爬取王者荣耀皮肤图片并保存至数据库和 Excel
本程序使用 Python 编写,实现爬取王者荣耀官网皮肤图片信息,并将其保存至 SQLite 数据库和 Excel 表格中,并生成图表或显示处理后的信息内容。
主要任务:
-
加载第三方库:
- requests: 用于发送 HTTP 请求
- BeautifulSoup4: 用于解析 HTML
- lxml: 用于解析 HTML (可选)
- sqlite3: 用于操作 SQLite 数据库
- jieba: 用于中文分词 (可选)
- WordCloud: 用于生成词云 (可选)
- openpyxl: 用于操作 Excel 表格
-
爬取网站信息:
- 爬取王者荣耀官网皮肤图片列表页
- 解析网页,提取英雄名称和皮肤图片链接
-
保存信息:
- 将提取的信息保存至 SQLite 数据库表中
- 将提取的信息保存至 Excel 表格中
-
数据处理和可视化:
- 可以根据需要对数据进行进一步处理,例如生成图表或词云等
代码示例:
import requests
from bs4 import BeautifulSoup
import sqlite3
import openpyxl
# 爬取王者荣耀皮肤图片
url = 'https://pvp.qq.com/web201605/herolist.shtml'
res = requests.get(url)
soup = BeautifulSoup(res.text, 'lxml')
hero_list = soup.find_all('li', class_='clearfix')
hero_info = []
for hero in hero_list:
hero_name = hero.find('a').text
hero_url = hero.find('img')['src']
hero_info.append((hero_name, hero_url))
# 将信息保存到sqlite数据库表中
conn = sqlite3.connect('heros.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS hero_info
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
url TEXT)''')
for info in hero_info:
c.execute('INSERT INTO hero_info (name, url) VALUES (?, ?)', (info[0], info[1]))
conn.commit()
conn.close()
# 将信息保存到Excel表中
wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'Hero Info'
for i, info in enumerate(hero_info):
ws.cell(row=i+1, column=1, value=info[0])
ws.cell(row=i+1, column=2, value=info[1])
wb.save('heros.xlsx')
注意:
- 爬取网站信息时,请遵守网站的 robots.txt 协议。
- 爬取频率过高可能会导致网站封锁您的 IP 地址。
- 您可以根据需要修改代码,实现更多功能。
希望本程序能够帮助您学习 Python 爬虫技术。
原文地址: http://www.cveoy.top/t/topic/oQhv 著作权归作者所有。请勿转载和采集!