ikkhksvu 发表于 2024-8-17 22:46:35

教你用 Python 下载手机小视频


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">今天再为<span style="color: black;">大众</span>介绍下<span style="color: black;">运用</span>这个工具<span style="color: black;">怎样</span>监控手机上网,并且<span style="color: black;">经过</span>抓包,把<span style="color: black;">咱们</span>想要的数据下载下来。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">起步</span> mitmproxy</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">首要</span><span style="color: black;">咱们</span><span style="color: black;">经过</span>执行命令 </span><span style="color: black;"><span style="color: black;">mitmweb</span></span><span style="color: black;"> <span style="color: black;">起步</span> </span><span style="color: black;"><span style="color: black;">mitmproxy</span></span><span style="color: black;">,让它处理监听状态,服务会监听本机 8080 端口,<span style="color: black;">起步</span>后如下:</span></p>$ mitmweb
    Web server listening at http:<span style="color: black;">//127.0.0.1:8081/</span>
    <span style="color: black;">Proxy</span> server listening at http:<span style="color: black;">//*:8080</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">手机网络配置</h1><span style="color: black;"><span style="color: black;">保准</span>手机和电脑在同一局域网内,并查看电脑的局域网 IP <span style="color: black;">位置</span>是多少以备用,查看方式<span style="color: black;">能够</span>用命令查看,如 Windows 系统用 </span><span style="color: black;"><span style="color: black;">ipconfig</span></span><span style="color: black;">命令,Mac 或 Linux 则用命令</span><span style="color: black;"><span style="color: black;">ifconfig</span></span><span style="color: black;">,看到如下图所示,找到本机在局域网的 IP <span style="color: black;">位置</span>。</span>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/100e36aee6ab43c18a743b5f40e8c307~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896508&amp;x-signature=Y9gqdrNVKndJyFk1I8oRf36UjQQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">区别</span>操作系统,可能展示结果<span style="color: black;">区别</span>,在<span style="color: black;">这儿</span><span style="color: black;">咱们</span><span style="color: black;">能够</span>看到红框里我本机的 IP <span style="color: black;">位置</span>为 192.168.0.108。</span></p><span style="color: black;">配置手机代理<span style="color: black;">位置</span>,<span style="color: black;">例如</span>以下用 iphone 手机进行配置,打开设置 -&gt; 无线局域网 -&gt; 点<span style="color: black;">此刻</span>连接的网络 -&gt; 点最下面点 HTTP 代理配置,<span style="color: black;">选取</span>手动后,配置代理<span style="color: black;">位置</span>为<span style="color: black;">咱们</span>电脑的 IP<span style="color: black;">位置</span>和 mitmproxy 的监听端口,配置如下图所示:</span>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/c4ca1ed1a3504818bb3cf8069c3cba3e~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896508&amp;x-signature=zjeas5xjkPLYyqT5z4HlNTEo3Rw%3D" style="width: 50%; margin-bottom: 20px;"></div><span style="color: black;"><span style="color: black;">此刻</span><span style="color: black;">咱们</span>就<span style="color: black;">能够</span>让手机<span style="color: black;">经过</span>电脑上网了,<span style="color: black;">不外</span><span style="color: black;">日前</span>手机 App 都是<span style="color: black;">经过</span> HTTPS 加密请求,<span style="color: black;">因此</span><span style="color: black;">咱们</span><span style="color: black;">必须</span>在手机上安装个 mitmproxy 的 HTTPS 证书。接下来<span style="color: black;">咱们</span>打开手机浏览器,输入网址:mitm.it,打开如下图所示页面,<span style="color: black;">选取</span>对应手机的操作系统,下载证书并安装。</span>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/f236600661054806a31124c060eeacc4~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896508&amp;x-signature=tohyjAnUmOnG1wKu9PqyZ4ZIiJw%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在<span style="color: black;">这儿</span>我点 Apple 的图标下载证书,下载后等待安装:</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/8ef9828001834c77ad691c80020e8b3b~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896508&amp;x-signature=k%2BoqlDvX3ssDtGlbh348Ul5m%2F1o%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">单击上面的安装,可能会<span style="color: black;">显现</span>信任的提示,只要<span style="color: black;">选取</span>信任证书就<span style="color: black;">能够</span>了,下面是安装好的界面如下图:</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/3bb34fed837e4292a38d4b5e979dc81c~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896508&amp;x-signature=n%2FSOfH5r3um3rZutBsiip1b%2BqWI%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">好了,配置完成下面<span style="color: black;">咱们</span>点开个手机 APP 看到电脑 mitmweb 的页面上,<span style="color: black;">已然</span><span style="color: black;">显现</span>请求内容了。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/76594b491f354d5d81263874588b159f~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896508&amp;x-signature=nr%2FDTKVJmV%2BwNiVz42WVddkw%2Bew%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">编写下载脚本</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">好了,<span style="color: black;">此刻</span>经过分析拦截的请求,<span style="color: black;">咱们</span><span style="color: black;">起始</span>用 Python 编写代码,<span style="color: black;">经过</span>解析视频的 url 将视频下载到本地。在<span style="color: black;">这儿</span><span style="color: black;">咱们</span>先来打印下数据,新建文件 </span><span style="color: black;"><span style="color: black;">xiaoshipin.py</span></span><span style="color: black;">,<span style="color: black;">而后</span>编写如下代码,实现将已编码的 json 字符串解码为 python 对象:</span></p><span style="color: black;"># xiaoshipin.py</span>

    <span style="color: black;">import</span> <span style="color: black;">json</span>

    <span style="color: black;">def</span> <span style="color: black;">response(flow):</span>
    <span style="color: black;">url</span>=<span style="color: black;">https://api.amemv.com/aweme/v1/aweme/post/</span>
    <span style="color: black;"> #筛选出以上面url为开头的url</span>
    <span style="color: black;">if</span> <span style="color: black;">flow.request.url.startswith(url):</span>
    <span style="color: black;">text</span>=<span style="color: black;">flow.response.text</span>
    <span style="color: black;"> #将已编码的json字符串解码为python对象</span>
    <span style="color: black;">data</span>=<span style="color: black;">json.loads(text)</span>
    <span style="color: black;">print(data)</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">编辑完后<span style="color: black;">保留</span>,<span style="color: black;">而后</span>执行命令:</span><span style="color: black;"><span style="color: black;">mitmweb -s xiaoshipin.py</span></span><span style="color: black;">,带上这段脚本来运行<span style="color: black;">咱们</span>的代理服务,刷新几个视频,会看到如下图所示内容:</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/3ee32d99329b451aa2546e44310331a1~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896508&amp;x-signature=NpCoXF1zssh0wZBoURmbTlz4Ny8%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">其中就<span style="color: black;">包括</span>了<span style="color: black;">咱们</span><span style="color: black;">必须</span>的下载<span style="color: black;">位置</span>信息,接下来<span style="color: black;">咱们</span>只需提取视频的 url ,并且缓存视频到本地就 OK 了。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下面<span style="color: black;">咱们</span>就来修改上面的 </span><span style="color: black;"><span style="color: black;">xiaoshipin.py</span></span><span style="color: black;"> 的代码,完善下视频下载的功能。</span></p><span style="color: black;">#xiaoshipin.py</span>

    <span style="color: black;">#引入包</span>
    <span style="color: black;">import</span> json,os
    <span style="color: black;">import</span> requests

    <span style="color: black;"># 只拦截并处理返回请求</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">response</span><span style="color: black;">(flow)</span>:</span>
    <span style="color: black;"># 请求的 url</span>
    url=<span style="color: black;">https://api.amemv.com/aweme/v1/aweme/post/</span>
    <span style="color: black;">#筛选出以上面url为开头的url</span>
    <span style="color: black;">if</span>flow.request.url.startswith(url):
    text=flow.response.text<span style="color: black;">#将已编码的json字符串解码为python对象</span>
    data=json.loads(text)
    <span style="color: black;"># 刚分析看到每一个视频的所有信息</span>
    <span style="color: black;"># 都在aweme_list中</span>
    video_url=data[<span style="color: black;">aweme_list</span>]
    <span style="color: black;"># 设置下载路径</span>
    path=<span style="color: black;">/Users/xx/shipin</span>
    <span style="color: black;"># <span style="color: black;">倘若</span>文件夹不存在,则新建</span>
    <span style="color: black;">if</span> <span style="color: black;">not</span> os.path.exists(path):
    os.mkdir(path)

    <span style="color: black;"># 循环所有视频 url</span>
    <span style="color: black;">for</span> each <span style="color: black;">in</span> video_url:
    <span style="color: black;">#视频描述</span>
    desc=each[<span style="color: black;">desc</span>]
    url=each[<span style="color: black;">video</span>][<span style="color: black;">play_addr</span>][<span style="color: black;">url_list</span>][<span style="color: black;">0</span>]
    <span style="color: black;"># 设置视频名<span style="color: black;">叫作</span></span>filename=path+<span style="color: black;">/</span>+desc+<span style="color: black;">.mp4</span>
    <span style="color: black;"># 用 request 请求视频流</span>
    req=requests.get(url=url,verify=<span style="color: black;">False</span>)
    <span style="color: black;"># <span style="color: black;">保留</span>视频文件</span>
    <span style="color: black;">with</span> open(filename,<span style="color: black;">ab</span>) <span style="color: black;">as</span>f:
    f.write(req.content)
    f.flush()
    print(filename,<span style="color: black;">下载完毕</span>)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">上面我<span style="color: black;">已然</span>把<span style="color: black;">关联</span>代码注释好了,<span style="color: black;">此刻</span><span style="color: black;">咱们</span><span style="color: black;">保留</span>编辑好的代码,<span style="color: black;">而后</span>再次执行命令:</span><span style="color: black;"><span style="color: black;">mitmweb -s xiaoshipin.py</span></span><span style="color: black;">,<span style="color: black;">起步</span>监听服务。<span style="color: black;">而后</span>打开手机再次下视频,就会看到视频是不是<span style="color: black;">已然</span>都存到本地了。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/e753ad04dccb487f9539caed4bc0b0fb~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896508&amp;x-signature=Wx7gbRYoyGgdXAqkGuSbbjINdVc%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">总结</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">本文为<span style="color: black;">大众</span>介绍了<span style="color: black;">怎样</span><span style="color: black;">经过</span> </span><span style="color: black;"><span style="color: black;">mitmproxy</span></span><span style="color: black;">工具下载手机上浏览的小视频,当然除了视频他还<span style="color: black;">能够</span>下载音乐啊<span style="color: black;">照片</span>啊什么的,只要你能<span style="color: black;">经过</span>拦截工具分析你想要的内容在哪个请求中,<span style="color: black;">而后</span>对这个请求返回内容进行搜索分析,<span style="color: black;">而后</span>再用今天这个工具,再<span style="color: black;">经过</span>简单的 Python 编码实现自动处理完成就<span style="color: black;">能够</span>了。</span></p>




jszmdq158 发表于 2024-9-9 18:03:39

认真阅读了楼主的帖子,非常有益。

1fy07h 发表于 2024-10-26 12:32:58

感谢楼主的分享!我学到了很多。

4lqedz 发表于 2024-10-27 17:59:21

你的留言真是温暖如春,让我感受到了无尽的支持与鼓励。
页: [1]
查看完整版本: 教你用 Python 下载手机小视频