python爬取王者荣耀英雄的背景故事 您所在的位置:网站首页 爬取数据分析王者荣耀存在的运营问题 python爬取王者荣耀英雄的背景故事

python爬取王者荣耀英雄的背景故事

2024-06-24 21:29| 来源: 网络整理| 查看: 265

文章目录 前言目标数据源分析代码实现过程1、代码框架2、获取英雄编号及名称数据3、获取英雄故事数据 完整代码 我只用了四个函数,《王者荣耀》就把每个英雄的背景故事递给了我 在这里插入图片描述

前言

学习爬虫,以下内容要学习:

成功安装了Python环境,这里我使用的是python 3.9能够熟练掌握一种IDE,这里我使用的是Pycharm能够熟练地安装第三方库,如requests库,但不限于此能够掌握一些python的基础语法知识能够养成遇到问题,多思考、多百度的习惯 目标数据源分析 目标地址:

目标地址1:https://pvp.qq.com/web201605/herolist.shtml 目标网址2:https://pvp.qq.com/web201605/herodetail/{英雄编号}.shtml

爬取目标:

全部王者英雄的英雄故事!

用到的基础库: import os import re import bs4 import requests import chardet # 可选,可不选 import logging # 可选,可不选 代码实现过程 1、代码框架

先看一下代码的整体结构: 在这里插入图片描述 这里我定义了三个全局变量,如果放到主函数里,可以使框架更清晰。

2、获取英雄编号及名称数据

首先,进入王者荣耀官网:https://pvp.qq.com/ 按照以下步骤打开一个新的页面,得到第一个目标网址。 在这里插入图片描述 接着,进行第一个内容的爬取,英雄的名称和编号:

那我首先要知道,这个东西在哪,对不对?

如图所示(本来录的GIF,结果放不出来):

在这里插入图片描述 再点击一下,便可以得到想要的URL 在这里插入图片描述

此处代码需要掌握的知识有:

requests库,re模块,正则表达式

import re import requests headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/' '537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36' } # 获取英雄名称及对应编号 def get_hero_num(url): response = requests.get(url=url, headers=headers).text hero_list = re.findall('"ename": (.+?),', response, re.S) # 得到英雄的编号列表 hero_name = re.findall('"cname": "(.+?)"', response, re.S) # 得到英雄的名字列表 return hero_name, hero_list def main(): url = 'https://pvp.qq.com/web201605/js/herolist.json' hero_name, hero_list = get_hero_num(url) print('英雄名称为:\n', hero_name) print('英雄编号为:\n', hero_list)

在这里插入图片描述 是可以成功获取的。

3、获取英雄故事数据

将英雄的编号,填入目标网址2对应的英雄编号处: https://pvp.qq.com/web201605/herodetail/{英雄编号}.shtml

然后就访问这个页面咯(先用新英雄云缨试一下,对应编号为538)

此处代码需要掌握的知识有:

requests库,bs4库,chardet库(可选,但建议学一下)

url = 'https://pvp.qq.com/web201605/herodetail/538.shtml'# 进入英雄详细页面的链接 res = requests.get(url=url, headers=headers) res.encoding = chardet.detect(res.content)['encoding'] # 统一字符编码,解决乱码问题 res = res.text print(res)

你看,这不就得到了吗 在这里插入图片描述 下面就是对这部分的数据进行清洗。

也很简单,利用“美丽的汤”–BeautifulSoup库,在上述代码加上这三句:

soup = bs4.BeautifulSoup(res, 'html.parser') story = soup.select('.pop-bd')[0].text print(story)

芜湖,这样就可以得到了 在这里插入图片描述 故事的展现有点问题,但影响不大,一会再优化。

完整代码

我对上面代码加了一点点,改动,并没有一次性爬取所有的英雄的故事,而是根据用户的输入进行指定爬取。

贴上结果先,嘻嘻~

在这里插入图片描述

代码如下:

# -*- coding: UTF-8 -*- # @Time: 2021/7/18 18:08 # @Author: 远方的星 # @CSDN: https://blog.csdn.net/qq_44921056 import os import re import bs4 import requests import chardet import logging # 日志输出的基本配置 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s') # 创建一个文件夹 path = './王者故事' if not os.path.exists(path): os.mkdir(path) headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/' '537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36' } # 获取英雄名称及对应编号 def get_hero_num(url, hero_dream): response = requests.get(url=url, headers=headers).text # print(response) hero_list = re.findall('"ename": (.+?),', response, re.S) # 得到英雄的编号列表 hero_name = re.findall('"cname": "(.+?)"', response, re.S) # 得到英雄的名字列表 hero_num = hero_name.index(hero_dream) num = hero_list[hero_num] # 得到英雄序号 return num # 根据编号获取英雄背景故事 def get_story(num): url = 'https://pvp.qq.com/web201605/herodetail/{}.shtml'.format(num) # 进入英雄详细页面的链接 res = requests.get(url=url, headers=headers) res.encoding = chardet.detect(res.content)['encoding'] # 统一字符编码,解决乱码问题 res = res.text soup = bs4.BeautifulSoup(res, 'html.parser') story = soup.select('.pop-bd')[0].text # 虚拟故事段 story = story.replace(' ', '\n').replace('”', '\n').replace(' ', '') story = story.encode(encoding='utf-8') return story # 下载故事 def download(hero_dream, story): # 下载函数 file_name = hero_dream+'.txt' file_path = path + '/' + file_name with open(file_path, 'wb') as f: f.write(story) logging.info('{}的故事已经下载完成啦!感谢您的使用~'.format(hero_dream)) f.close() def main(): hero_dream = input("请输入你想查看的英雄故事:") url = 'https://pvp.qq.com/web201605/js/herolist.json' num = get_hero_num(url, hero_dream) story = get_story(num) download(hero_dream, story) if __name__ == '__main__': main()

希望能对你有所帮助~~~

所以,我有故事,你有酒吗?

作者:远方的星 CSDN:https://blog.csdn.net/qq_44921056 本文仅用于交流学习,未经作者允许,禁止转载,更勿做其他用途,违者必究。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有