• python带你采集不可言说网站数据,并带你多重骚操作~


    前言

    嗨喽,大佬们好鸭!这里是小熊猫🖤~

    今天我们采集国内知名的shipin弹幕网站!

    这里有及时的动漫新番,活跃的ACG氛围,有创意的Up主。

    大家可以在这里找到许多欢乐。

    image.png

    python方向

    • 爬虫程序 >>> 爬虫工程师 采集数据处理
    • 网站开发 >>> 开发工程师
    • 数据分析 >>> 数据分析师 处理表格 word
    • 自动化
    • 游戏开发/辅助
    • 人工智能/机器学习 深度学习
    • 自动化测试 运维

    环境使用:

    • Python 3.8
    • Pycharm 2021.2版本
    • ffmpeg

    <需要设置环境变量> 软件的使用 合成视频和音频

    需要私我领取

    模块使用:

    需要安装模块

    • import requests >>> pip install requests

    内置模块 你安装好python环境就可以了

    • import re
    • import json
    • import subprocess

    秘籍 <适用于任何网站, 采集任何数据>

    一. 数据来源分析 点击此处领取免费资料

    1. 确定自己需求 <采集网站是那个, 获取数据是什么东西>
    2. 通过开发者工具进行抓包分析, 分析我们想要数据内容来自于哪里 <通过网页源代码就可以找到相应数据内容>
      shipin信息数据 在网页源代码里面 playinfo里面
      shipin标题

    我们想要数据内容都是来自于网页源代码

    I. 用浏览器打开这个所采集的网址

    II. 在这个网页上面用鼠标右键点击查看网页源代码 会弹出一个新的窗口

    III. ctrl + F 打开搜索框 搜索playinfo 可以找到相关信息数据

    IV. ctrl + F 打开搜索框 搜索shipin标题, 也可以找到相关的数据内容

    二. 代码实现步骤过程 基本四大步骤

    1. 发送请求, 模拟浏览器对于url网址发送请求 <专门定义函数来发送请求>
    2. 获取数据, 获取网页源代码
    3. 解析数据, 提取我们想要数据内容 <shipin信息以及shipin标题>
    4. 保存数据, 把shipin内容保存本地

    代码 点击此处领取免费资料

    1. 导入所需模块

    不是内置模块的小可耐们记得安装哦~

    import requests  # 数据请求模块 <发送请求工具>
    import re  # 正则表达式
    import json  # 序列化与反序列
    import pprint  # 格式化输出模块
    import subprocess
    import os
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    因审核机制原因,我把网址里的一些东西删掉了,小可耐们可以自己添加一下哈,很容易的

    还有两个字,我用拼音代替了,你们可以改回文字哦 ~

    如果有不太会改或者有点点小懒惰的小可耐也可以私信我,我发你呐~

    2. 模拟浏览器发送请求

    def get_response(html_url, data=None):
        """
        发送请求函数
        def 关键字 用于自定义函数 get_response 自定义函数名字
        :param html_url:  形式参数<不具备实际意义> 传入到这个函数参数  发送请求网址
        :return: 响应对象
    
        模拟浏览器发送请求
            headers 请求头进行伪装模拟 user-agent 用户代理意思 表示浏览器基本身份标识
        <Response [200]>   <>在python里面表示对象 响应对象 200 状态码 表示请求成功
    
        相当于打电话, 打通了嘟嘟嘟声音
        404 网址不对      相当于你所拨打电话是空号
        503 服务器有问题   相当于你所拨打电话不在服务区
        403 你没有访问权限  相当于你所拨打电话正在通话中
        """
        headers = {
            'referer': 'https://www..com/',  # 防盗链 告诉服务器你请求url是从哪里跳转过来的
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36'
        }
        response = requests.get(url=html_url, params=data, headers=headers)
        return response
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3. 获取数据 点击此处领取免费资料

    def get_video_info(play_url):
        """
        获取shipin信息函数
        :param play_url: shipin播放详情页
        :return: 信息shipin信息
        仰望星空脚踏实地
        不会用正则 6 通过相关语法,可以对于字符串数据提取自己想要内容 解析数据
        re.findall() 通过re模块里面findall 去找到所有我们想要数据内容
            从什么地方去找什么样数据内容  从response.text里面去找 "title":"(.*?)","pubdate" 中(.*?) 这段是我们想要数据
            '"title":"(.*?)","pubdate"', response.text
        保存数据, 标题作为文件名保存, 以字符串数据形式 列表取值
        len 统计元素个数 [0] 根据索引位置取值 列表索引位置从0开始计数
        type() 内置函数,查看数据类型
        """
        # 定义了函数, 一定要调用
        response = get_response(html_url=play_url)  # 调用前面定义好的发送请求函数 函数是可以重复调用
        # print(response.text)  # response.text 获取响应对象文本数据 <获取网页源代码>  字符串数据
        title = re.findall('"title":"(.*?)","pubdate"', response.text)[0].replace(' ', '')  # 标题
        title = re.sub(r'[/\:*?"<>|]', '', title)
        html_data = re.findall('<script>window.__playinfo__=(.*?)</script>', response.text)[0]  # 视频信息
        # print(title)
        # print(html_data)
        # print(type(html_data))
        # 为了方便提取数据, 可以把这个html_data 转成json字典数据
        json_data = json.loads(html_data)
        # print(json_data)
        # print(type(json_data))  # 输出一行
        # 字符串单双引号使用  外面是单引号里面就要使用双引号
        # pprint.pprint(json_data)  # 格式化展开效果
        # 字典取值, 键值对取值 根据冒号左边的内容<键>, 提取冒号右边的内容<值>
        audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
        video_url = json_data['data']['dash']['video'][0]['baseUrl']
        print(audio_url)
        print(video_url)
        video_info = [title, audio_url, video_url]
        return video_info
    
    • 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

    4. 保存数据 点击此处领取免费资料

    def save(title, audio_url, video_url):
        """
        保存数据函数
        :param title: shipin标题
        :param audio_url: 音频url
        :param video_url: shipin画面url
        :return:
        """
        audio_content = get_response(html_url=audio_url).content
        video_content = get_response(html_url=video_url).content
        with open('video\\' + title + '.mp3', mode='wb') as f:
            f.write(audio_content)
        with open('video\\' + title + '.mp4', mode='wb') as f:
            f.write(video_content)
        cmd = f"ffmpeg -i video\\{title}.mp4 -i video\\{title}.mp3 -c:v copy -c:a aac -strict experimental video\\{title}output.mp4"
        subprocess.run(cmd, shell=True)
        os.remove(f'video\\{title}.mp4')
        os.remove(f'video\\{title}.mp3')
        print('')
        print(title, '视频下载完成')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    5. 实现搜索功能

    def get_search(page, word):
        """
    
        :param page: 采集多少页
        :param word: 搜索关键字
        :return:
        """
        search_url = 'https://api..com/x/web-interface/search/type'
        data = {
            '__refresh__': 'true',
            '_extra': '',
            'context': '',
            'page': page,
            'page_size': '42',
            'from_source': '',
            'from_spmid': '333.337',
            'platform': 'pc',
            'highlight': '1',
            'single_column': '0',
            'keyword': word,
            'category_id': '',
            'search_type': 'video',
            'dynamic_offset': '84',
            'preload': 'true',
            'com2co': 'true',
        }
        json_data = get_response(html_url=search_url, data=data).json()
        bv_list = [i['bvid'] for i in json_data['data']['result']]
        print(bv_list)
        return bv_list
    
    # 6. 采集多个shipin
    def get_up_video(page, up_id):
        """
        采集up多个shipin
        :param page: 采集多少页
        :param up_id: shipin博主ID
        :return:
        """
        up_link = 'https://api..com/x/space/arc/search'
        data = {
            'mid': up_id,
            'ps': '30',
            'tid': '0',
            'pn': page,
            'keyword': '',
            'order': 'pubdate',
            'jsonp': 'jsonp',
        }
        json_data = get_response(html_url=up_link, data=data).json()
        bv_list = [i['bvid'] for i in json_data['data']['list']['vlist']]
        print(bv_list)
        return bv_list
        
    # 7. 获取id
    def main(bv_id):
        """
        主函数
        :param bv_id: shipin bv号
        :return:
        """
        video_info = get_video_info(play_url=f'https://www..com/video/{bv_id}')
        save(video_info[0], video_info[1], video_info[2])
    
    • 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
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    8. 一个简单的操作界面 点击此处领取免费资料

    if __name__ == '__main__':
        # 只要你可以看到数据
        msg = """请输入你要进行的操作:
            A. 多页数据采集
            B. 采集单个shipin
            C. 采集番剧
            D. 采集某个Up所有shipin内容
            0. 即可退出系统
        """
        while True:
            print(msg)
            kew_word = input('请输入你要进行的操作: ')
            if kew_word == 'A' or kew_word == 'a':
                word = input('请输入你想要下载shipin关键字: ')
                page = input('请输入你想要下载shipin页数: ')
                for num in range(1, int(page) + 1):
                    bv_list = get_search(page=num, word=word)
    
            elif kew_word == 'B' or kew_word == 'b':
                bv = input('请输入你想要下载shipinBv号: ')
                
    
            elif kew_word == 'C' or kew_word == 'c':
                print('功能还没上线')
    
            elif kew_word == 'D' or kew_word == 'd':
                up_id = input('请输入你想要下载up主ID: ')
                page = input('请输入你想要下载shipin页数: ')
                for num in range(1, int(page) + 1):
                    get_up_video(page=num, up_id=up_id)
    
            elif kew_word == '0':
                break
    
    
    • 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

    尾语

    成功没有快车道,幸福没有高速路。

    所有的成功,都来自不倦地努力和奔跑,所有的幸福都来自平凡的奋斗和坚持

    ——励志语录

    本文章就写完啦~感兴趣的小伙伴可以复制代码去试试

    你们的支持是我最大的动力!!记得三连哦~ 欢迎大家阅读往期的文章呀~

    我是小熊猫, 咱们下篇文章见啦🖤

  • 相关阅读:
    SM5308移动电源芯片
    数据库去除重复数据(id除外)
    算法-26. 删除有序数组中的重复项-⭐
    基于51单片机的温度报警系统
    我的IC秋招小结
    CM72 另类加法
    批量多字段唯一性校验
    oracle系统负载指标DB_TIME
    神经网络与深度学习——第4章 前馈神经网络
    项目延期常见的三个原因及对应的解决方法
  • 原文地址:https://blog.csdn.net/m0_67575344/article/details/125415365