关键词搜索

源码搜索 ×
×

Python制作吃鸡各数据资料查询助手,带你做理论王者~

发布2022-11-23浏览539次

详情内容

前言

大家早好、午好、晚好吖 ❤ ~

吃鸡想必大家都玩过了

今天来教大家制作一个资料查询助手

1、我们是不是要去获取这些数据 武器配件

首先:对于 武器一个详情页url地址发送请求, 获取 每个武器的url地址

其次:对于 每个武器的url地址发送请求 然后获取每个武器的一些基本信息

2、代码实现思路

1. 发送请求

  • url 唯一资源定位

  • 请求头 headers 字典形式

  • 请求体

    注意点: headers参数问题

  • 请求方式:get请求 / post请求

2. 获取数据

遇到到反pa怎么办,遇到加密怎么办:

字体加密、JS加密、动态数据网页参数变化怎么找,在哪找

  • response.text:获取网页的文本数据、字符串

  • json() :json字典数据怎么取值? 根据键值对取值

  • content

状态码

3. 解析数据

方式很多种:

  • 正则表达式 re

  • bs4

  • xpath

  • parsel (css选择器/xpath)

4. 保存数据 (只要打印输入就可以了)

  • 保存文本

  • 保存json

  • 保存数据库:

    非关系型数据库

    关系型数据库

开始敲代码

需要采集的数据:武器、配件、物资、载具


在发送请求之前是不是需要加一个请求头

请求头: 把python代码伪装成浏览器对服务器发送一个请求 然后服务器就会给我们返回一个response数据

user-agent :浏览器信息

import requests # 第三方模块
 
headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
}
response = requests.get(url=html_url, headers=headers)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

先采集解析武器的数据,优缺点、武器的伤害都全部采集下来

源码、素材电子书点击这里

def get_arms_info():
  • 1

    response = get_response(html_url=url)
    selector = parsel.Selector(response.text)
    # css选择器 就根据标签属性提取相关内容
    href = selector.css('#section-container .clear li a::attr(href)').getall()
    titles = selector.css('#section-container .clear li a::attr(title)').getall()
    # 通常我们要获取一个列表里面 每个元素 是不是要通过遍历 for循环
    zip_data = zip(href, titles)
    lis = []
    for index in zip_data:
        dit = {
            '物品名称': index[1],
            '详情页': index[0]
        }
        lis.append(dit)
    pd_data = pd.DataFrame(lis)
    pd.set_option('display.max_columns', None)
    print(pd_data)
    arms_num = input('请输入你要查询的武器序号: ')
    if int(arms_num) <= len(lis):
        arms_url = lis[int(arms_num)]['详情页']
        response_1 = get_response(arms_url)
        selector_1 = parsel.Selector(response_1.text)
        kind = selector_1.css('.wea_class::text').get() # 武器种类
        bullet = selector_1.css('.wea_bullet::text').get() # 子弹口径
        skin_list = selector_1.css('.parts_list li .skin_name::text').getall() # 子弹口径
        # 把列表转成我们字符串类型
        skin_name = '/'.join(skin_list)
        advantage = selector_1.css('.merit_text p:nth-child(2)::text').get()
        defect = selector_1.css('.merit_text p:nth-child(4)::text').get()
        st_hurt = selector_1.css('.merit_rt_st li::text').getall()
        tb_hurt = selector_1.css('.merit_rt_tb li::text').getall()
        print('--'*50)
        print('武器名字: ', lis[int(arms_num)]['物品名称'])
        print('武器的类型: ', kind)
        print('子弹', bullet)
        print('最佳配件: ', skin_name)
        print('优点: ', advantage)
        print('缺点: ', defect)
        print('--'*50)
        print('武器击中身体伤害:')
        print(f'裸装击中身体:{st_hurt[0]}枪淘汰')
        print(f'一级甲击中身体:{st_hurt[1]}枪淘汰')
        print(f'二级甲击中身体:{st_hurt[2]}枪淘汰')
        print(f'三级甲击中身体:{st_hurt[3]}枪淘汰')
        print('--' * 50)
        print('武器击中头部伤害:')
        print(f'裸装击中头部:{tb_hurt[0]}枪淘汰')
        print(f'一级头击中头部:{tb_hurt[1]}枪淘汰')
        print(f'二级头击中头部:{tb_hurt[2]}枪淘汰')
        print(f'三级头击中头部:{tb_hurt[3]}枪淘汰')
        print('--' * 50)
    else:
        print('输入有误')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

配件的数据解析

def get_fitting_info():
  • 1

“”“配件”“”

    response = get_response(html_url)
    selector = parsel.Selector(response.text)
    titles = selector.css('#section-container2 .clear li a::attr(title)').getall()
    href = selector.css('#section-container2 .clear li a::attr(href)').getall()
    zip_data_1 = zip(titles, href)
    lis = []
    for index in zip_data_1:
        title = index[0]
        index_url = index[1]
        dit = {
            '物品名称': title,
            '详情页': index_url,
        }
        lis.append(dit)
    pd_data = pd.DataFrame(lis)
    pd.set_option('display.max_columns', None)
    print('配件分类如下所示:')
    print(pd_data)
    fitting_num = input('请输入你要查询的配件序号:')
    fitting_url = lis[int(fitting_num)]['详情页']
    html_data = get_response(fitting_url).text
    sel = parsel.Selector(html_data)
    fitting_sx = sel.css('.intro_sx dd::text').get()
    fitting_sy = sel.css('.intro_sy dd::text').get()
    print('--' * 50)
    print('配件名字:', lis[int(fitting_num)]['物品名称'])
    print('配件属性:', fitting_sx)
    print('配件适用:', fitting_sy)
    print('--' * 50)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

物资的数据解析

def get_supplies_info():
  • 1

“”“物资”“”

    response = get_response(html_url)
    selector = parsel.Selector(response.text)
    titles = selector.css('#section-container3 .clear li a::attr(title)').getall()
    href = selector.css('#section-container3 .clear li a::attr(href)').getall()
    zip_data_2 = zip(titles, href)
    lis = []
    for index in zip_data_2:
        title = index[0]
        index_url = index[1]
        dit = {
            '物品名称': title,
            '详情页': index_url,
        }
        lis.append(dit)
    pd_data = pd.DataFrame(lis)
    pd.set_option('display.max_columns', None)
    print('物资分类如下所示:')
    print(pd_data)
    supplies_num = input('请输入你要查询的物资序号:')
    supplies_url = lis[int(supplies_num)]['详情页']
    html_data = get_response(supplies_url).text
    sel = parsel.Selector(html_data)
    supplies_sx = sel.css('.intro_sx dd::text').get()
    print('--' * 50)
    print('配件名字:', lis[int(supplies_num)]['物品名称'])
    print('配件属性:', supplies_sx)
    print('--' * 50)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

载具的数据解析

def get_car_info():
  • 1

“”“载具”“”

    response = get_response(html_url)
    selector = parsel.Selector(response.text)
    titles = selector.css('#section-container4 .clear li a::attr(title)').getall()
    href = selector.css('#section-container4 .clear li a::attr(href)').getall()
    zip_data_2 = zip(titles, href)
    lis = []
    for index in zip_data_2:
        title = index[0]
        index_url = index[1]
        dit = {
            '物品名称': title,
            '详情页': index_url,
        }
        lis.append(dit)
    pd_data = pd.DataFrame(lis)
    pd.set_option('display.max_columns', None)
    print('物资分类如下所示:')
    print(pd_data)
    supplies_num = input('请输入你要查询的物资序号:')
    supplies_url = lis[int(supplies_num)]['详情页']
    html_data = get_response(supplies_url).text
    sel = parsel.Selector(html_data)
    supplies_sx = sel.css('.intro_sx dd::text').get()
    print('--' * 50)
    print('配件名字:', lis[int(supplies_num)]['物品名称'])
    print('配件属性:', supplies_sx)
    print('--' * 50)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

调用函数,判断

if __name__ == '__main__':
    while True:
        string = """===================================
            和平精英资料查询助手V1.0版本
            0.武器 1.配件 2.物资 3.载具
==================================="""
        print(string)
        word = input('请输入你要查询的内容(输入n退出): ')
        if word == '0':
            get_arms_info()
        elif word == '1':
            get_fitting_info()
        elif word == '2':
            get_supplies_info()
        elif word == '3':
            get_car_info()
        elif word == 'n':
            break
        else:
            print('请正确输入~~')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

结果展示

尾语 ?

好了,今天的分享就差不多到这里了!

完整代码及国内疫情数据抓取代码、视频讲解直接点击下方自取即可。

点击 蓝色字体 自取,我都放在这里了。

宁外给大家推荐一个好的教程:
【48小时搞定全套教程!你和大佬只有一步之遥【python教程】

有更多建议或问题可以评论区或私信我哦!一起加油努力叭(ง •_•)ง

喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!

请添加图片描述

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载