mac下使用pyhon+mimtdump 爬取m3u8

pyhon代碼

test.py文件

# -*- coding: utf-8 -*-
import json
import threading

import requests
import subprocess, sys, os


def check_json_format(raw_msg):
    """
    用於判斷一個字符串是否符合Json格式
    :param self:
    :return:
    """
    if isinstance(raw_msg, str):  # 首先判斷變量是否爲字符串
        try:
            json.loads(raw_msg, encoding='utf-8')
        except ValueError:
            return False
        return True
    else:
        return False


def response(flow):
	#例如:
    # https://open.com/43567890/TYUI678/678iu.m3u8
    # http://abc.com/api/play/5678?uuid=tyu567tyu789&device=0

    url = 'api/play'
    if url in flow.request.url:
        text = flow.response.text
        if check_json_format(text):
            data_json = json.loads(text)
            if data_json and data_json.get('resp').get('path'): #判斷json中有這些節點
                rescont = data_json.get('resp')
                videopath = rescont.get('path')
                title = rescont.get('title')
                print(videopath)
                filename = title + '.mp4'
                print(filename)
                t = threading.Thread(target=exec_cmd(videopath, filename))  # 創建一個線程
                t.start()  # 啓動線程
                print( filename,'下載完成')


def exec_cmd(videopath, filename):
    cmd = 'ffmpeg -i ' + videopath + ' -c copy ' + filename
    print(cmd)
    cmd = cmd.encode(sys.getfilesystemencoding())
    print(cmd)
    subprocess.call(cmd, shell=True)

MAC下準備工作

  • 安裝m3u8合成工具
    brew install ffmpeg
  • 安裝代理工具
    brew install mitmproxy

使用方法

  • 在bash下運行:
    mitmdump -s test.py -p 8888
  • 連代理
    把手機wifi連到電腦上(查電腦ip,如192.168.0.xxx),端口8888
    1.修改網絡
    2.顯示高級選項
    3.選代理、手動,填ip、端口

關於ssl(報錯問題)

連上代理後,手機瀏覽器打開http://mitm.it/按頁面說明步驟操作(ios,android各不同)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章