资源简介
spider_LOL.py
代码片段和文件信息
“““
# 1.爬取网页
# 2.解析数据
# 3.保存数据
“““
from bs4 import BeautifulSoup # 网页解析,获取数据
import re # 正则表达式,进行文字匹配
import urllib.request
import urllib.error # 指定URL,获取网络数据
import xlwt # 进行excel操作
import sqlite3 # 进行SQLite数据库操作
import json # json类型解码
import requests
import os
def main():
“““
主函数
:return:
“““
# base_url = “https://lol.qq.com/data/info-heros.shtml?id=“
base_url = “https://game.gtimg.cn/images/lol/act/img/js/hero/“
data_list = get_data(base_url)
save_path = ‘.\\LOL英雄资料.xls‘
print(data_list)
save_data(data_list save_path)
save_image(data_list)
# 正则表达式匹配查找所有英雄id
find_hero_id = re.compile(r‘{“heroId“:“(\d*)“‘) # 英雄id
find_name = re.compile(r‘“name“:“(.*)““alias‘) # 英雄名字
find_title = re.compile(r‘“title“:“(.*)““roles‘) # title
find_alias = re.compile(r‘“alias“:“(.*)““title‘) # 英雄别名
find_roles = re.compile(r‘“roles“:(.*)“shortBio‘) # roles
find_skin_id = re.compile(r‘“skinId“:“(\d*)“‘) # 皮肤id
find_chromas_BelongId = re.compile(r‘“chromasBelongId“:“(\d*)“‘)
def get_data(base_url):
“““
获得数据并解析
:return:
“““
data_list = []
list_url = “https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js“
hero_list = ask_url(list_url)
# 解析网页获取英雄id
js = json.dumps(hero_list sort_keys=True ensure_ascii=False indent=3)
js = json.loads(js encoding=“utf-8“)
# print(js)
hero_id = re.findall(find_hero_id js)
# print(hero_id)
# for i in range(100 101):
for i in range(len(hero_id)):
data = []
data.append(hero_id[i]) # 添加英雄id
url = base_url + hero_id[i] + “.js“
html = ask_url(url)
# 英雄名字
name = re.findall(find_name html)[0]
# strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
# split() 方法可以实现将一个字符串按照指定的分隔符切分成多个子串,这些子串会被保存到列表中(不包含分隔符),作为方法的返回值反馈回来
# print(name.strip().split(r‘\u‘))
# print(name.encode(‘unicode_escape‘).decode(‘utf8‘))
temp_name = name.split(r‘\u‘)[1:]
# print(‘‘.join([chr(int(s 16))for s in temp_name]))
hero_name = ‘‘.join([chr(int(s 16))for s in temp_name])
data.append(hero_name) # 添加英雄名字
# print(hero_name)
# 英雄title
title = re.findall(find_title html)[0]
temp_title = title.split(r‘\u‘)[1:]
hero_title = ‘‘.join([chr(int(s 16))for s in temp_title])
data.append(hero_title)
# print(hero_title)
#
相关资源
- python+ selenium教程
- 英文原版-Scientific Computing with Python
- CpuMemSets在Linux操作系统中的实现
- Python学习全系列教程永久可用
- 蓝奏云批量上传工具.zip
- python书籍 PDF
- 老男孩python项目实战
- Python.rar99111
- decision_tree_v2.py
- Python绝技运用Python成为顶级黑客.pdf
- python小波包文档及论文.zip
- Python黑帽子(黑客与渗透测试编程之
- FlaskWeb开发:基于Python的Web应用开发实
- Python基础教程第3版中英文源码.rar
- python数据结构与算法中文版.zip
- Python-冲顶大会芝士超人西瓜视频头脑
- time_series_forecasting_with_python.zip
- Python基础教程第三版PDF高清可复制.
- python编程从入门到实践.zip237878
- FlaskWeb开发:Python基于Web应用开发实战
- pythonBCRMDSJ.mobi
- 量化交易之路用Python做股票量化分析
- PYTHON自然语言处理中文版.pdf
- Python基础教程(第3版).rar
- GRAYHATPYTHON高清.英文.书签版.pdf
- Python简明教程第四版.rar
- Python编程:从入门到实践带书签完整
- Python基础教程(第3版).pdf109608
- vamei-从Python开始学编程.pdf
- 利用Python进行数据分析.pdf
评论
共有 条评论