美文网首页
Python(六十六)json模块、抖音视频无水印提取

Python(六十六)json模块、抖音视频无水印提取

作者: Lonelyroots | 来源:发表于2022-02-25 22:45 被阅读0次

    1. json模块

    05_json模块、抖音视频无水印提取/01_json模块1.py:

    """
    
        爬虫中数据分类:
            结构化数据:
                json、xml
            非结构化数据:html
                处理方法:正则表达式、xpath、css选择器
        json模块是Python内置模块
    
    """
    
    """
    
        JSON的判断标准:
            1. JSON不允许key为元组,但是Python的字典允许
            2. JSON中没有单引号的存在
        
        json.dumps()转JSON格式的字符串
        
        NOT JSON:
            {'a': 'A', 'c': 'C', 'b': 'B', ('2', 1): '元组', 22: '数字'}
        YES JSON:
            {"a": "A", "c": "C", "b": "B", "22": "数字"}
    
    """
    
    import json
    
    dict2 = {
        "name":"Tom","age":23
    }
    print(str(dict2))   # 打印{'name': 'Tom', 'age': 23},虽然可以 但不是json格式,不是json类型
    
    data1 = json.dumps(dict2)       # 转json格式的字符串
    print(data1)        # 打印{"name": "Tom", "age": 23}
    print(type(data1))      # 字符串类型<class 'str'>
    
    print("------------")
    
    data2 = json.dumps([])
    print(data2)        # 打印[]
    print(type(data2))      # 打印<class 'str'>
    
    data3 = json.dumps(2)
    print(type(data3))      # 打印<class 'str'>
    
    data4 = json.dumps("3")
    print(type(data4))      # 打印<class 'str'>
    
    data = {
        "a":"A","c":"C","b":"B"
    }
    print("排序:",json.dumps(data,sort_keys=True))
    print("缩进:",json.dumps(data,indent=3))
    print("紧凑:",json.dumps(data,separators=(',',':')))      # 把, 换成,;把: 换成:。
    
    data[('2',1)] = "元组"
    print(data)     # 打印{'a': 'A', 'c': 'C', 'b': 'B', ('2', 1): '元组'}
    
    data[22] = "数字"
    # data[[1,2]] = "数组"      # 字典的key不能为数组
    print(data)
    print(json.dumps(data,skipkeys=True))        # skipkeys 跳过异常过滤
    print(json.dumps(data,skipkeys=True,ensure_ascii=False))        # skipkeys:跳过异常过滤;ensure_ascii:解决编码问题。
    
    
    with open("test.json","w",encoding='utf-8') as fp:
        # fp.write(str(data))      # 可以写入,但不是json格式,这里传的data是字典
        # fp.write(json.dumps(data,skipkeys=True,ensure_ascii=False))
    
        # 与fp.write()作用差不多
        json.dump(data,fp,skipkeys=True,ensure_ascii=False)
    

    05_json模块、抖音视频无水印提取/02_json模块2.py:

    """
    
        总结:
            json.dumps() 将 Python对象编码成 JSON字符串,严格按照JSON格式进行转换:PythonDict -> jsonStr
            json.loads() 将 已编码的 JSON字符串解码为Python对象 jsonStr -> PythonDict
            json.dump() 和 json.load(),需要传入文件描述符,加上文件操作
    
    """
    
    import json
    
    dict2 = '{"name":"Tom","age":23}'       # 必须要是json格式的字符串
    data1 = json.loads(dict2)       # 将字符串还原为Python——字典dict
    print(data1)
    
    with open("test.json",'r',encoding='utf-8') as fp:
        print(fp.read())    # {"a": "A", "c": "C", "b": "B", "22": "数字"}
        print(type(fp.read()))      # <class 'str'>
        fp.seek(0)      # 光标移动到最前面
        data2 = json.loads(fp.read())       # 转成了Python可以操作的字典
        print(data2['22'],type(data2))      # 打印:数字 <class 'dict'>
        fp.seek(0)
        print(json.load(fp))      # {'a': 'A', 'c': 'C', 'b': 'B', '22': '数字'}
    

    2. jsonpath模块

    05_json模块、抖音视频无水印提取/03_jsonpath.py:

    """
    
        pip install jsonpath
            用来解析多层嵌套的json数据:jsonpath是一种信息抽取库
            
    """
    import jsonpath
    
    data = {
        "price":666,
        "store":{
            "author":"Lonelyroots",
            "product":"washing_machine",
            "price":5.5,
            "book":[{
                "category":"reference",
                "author":"Nigel Rees",
                "title":"Sayings of the Century",
                "price":8.95
            },{
                "category":"reference",
                "author":"Nigel Rees",
                "title":"Sayings of the Century",
                "price":12.99
            },{
                "category":"fiction",
                "author":"Herman Melville",
                "title":"Moby Dick",
                "isbn": "0-553-21311-3",
                "price":8.99
            },{
                "category":"fiction",
                "author":"Nigel Rees",
                "title":"Sayings of the Century",
                "isbn":"0-395-19395-8",
                "price":22.99
            }],
            "bicycle":{
                "color":"red",
                "price":19.95
            }
        }
    }
    
    print(data['store']['book'][0]['price'])    # 打印8.95
    print(jsonpath.jsonpath(data,'$..price'))   # 所有price节点,打印[666, 5.5, 8.95, 12.99, 8.99, 22.99, 19.95]
    print(jsonpath.jsonpath(data,'$.store.book[*].price'))   # book的所有price节点,两个点(使用时表示不知道有多少层),打印[8.95, 12.99, 8.99, 22.99]
    print(jsonpath.jsonpath(data,'$.price'))   # 打印第一层节点,打印[666]
    print(jsonpath.jsonpath(data,'$.store.*'))   # 打印store下的所有节点
    print(jsonpath.jsonpath(data,'$.store..price'))   # 打印[5.5, 8.95, 12.99, 8.99, 22.99, 19.95]
    print(jsonpath.jsonpath(data,'$..book[3]'))   # 打印[{'category': 'fiction', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'isbn': '0-395-19395-8', 'price': 22.99}]
    print(jsonpath.jsonpath(data,'$..book[(@.length-1)]'))   # 匹配倒数第一个book节点(写法一),打印[{'category': 'fiction', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'isbn': '0-395-19395-8', 'price': 22.99}]
    print(jsonpath.jsonpath(data,'$..book[-1:]'))   # 匹配倒数第一个book节点(写法二),打印[{'category': 'fiction', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'isbn': '0-395-19395-8', 'price': 22.99}]
    print(jsonpath.jsonpath(data,'$..book[:2]'))   # 匹配book的0与1节点,打印[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 12.99}]
    print(jsonpath.jsonpath(data,'$..book[?(@.isbn)]'))   # 匹配book中包含isbn的节点,打印[{'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': 'fiction', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'isbn': '0-395-19395-8', 'price': 22.99}]
    print(jsonpath.jsonpath(data,'$..book[?(@.price<10)]'))   # 匹配book中price小于10的节点,打印[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}]
    

    3. 抖音视频无水印提取

    05_json模块、抖音视频无水印提取/04_抖音无水印视频提取.py:

    """
        
        抖音无水印视频提取:(分享链接)
            https://v.douyin.com/JcWgevN/
            https://v.douyin.com/JcWBkvA/
    
        通过分享链接进入,刷新抓取Media路由
        https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0300f670000c09rpp10r57bgg9sijlg&ratio=720p&line=0
        https://aweme.snssdk.com/aweme/v1/playwm/?video_id=vodeof6fo000co9tu3c8bl9edene3td0&ratio=720p&line=0
        
        分析1:
            我们通过直接访问分享链接返回的内容中,没有我们需要的数据
        分析2:
            【只要页面上有数据,且不在正常的页面中,那么这个数据肯定存在于某个请求中】
            上面得知,直接访问链接是没用的,所以我们需要利用抓包工具,抓取到一个json数据中有我们需要的数据
            https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=6923085141166361870
            https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=6923122237490924815
            从两个URL可以看出,只有items_ids是不一样的,由此可以推测可能是视频的唯一ID        
        分析3:
            上面URL中items_ids参数是未知的,所以我们需要从别的地方找到一样的items_ids
            在分析1中,我们可以发现,html返回的内容对我们没用,但是!!重定向之后的URL中有分析2中需要的参数
        分析4:
            获取到视频的JsonUrl之后,我们就可以获取json数据
            再从JSON数据中找到videoURL
        分析5:
            通过上面拿到视频URL之后,我们发现这个视频还是有水印的!!!
            但是!!!我们只要把URL中的 playwm 改成 play 就没有水印了,(之前抖音内部人员透露的,不然我们发现不了),如下方链接:
            Google访问不了,Firefox可以访问
            修改前(有水印):https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0300f670000c09rpp10r57bgg9sijlg&ratio=720p&line=0
            修改后(无水印):https://aweme.snssdk.com/aweme/v1/play/?video_id=v0300f670000c09rpp10r57bgg9sijlg&ratio=720p&line=0
    
    """
    from requests_html import HTMLSession
    import re
    from jsonpath import jsonpath
    import json
    
    session = HTMLSession()
    shareUrl = "https://v.douyin.com/JcWgevN/"
    response = session.get(url=shareUrl)
    # print(response.text)      # html对我们没用,将运行结果复制粘贴到 文件04_抖音无水印提取视频.html
    PageUrl = response.url     # 把重定向的路由拿出来
    # print(PageUrl)      # https://www.douyin.com/video/6923085141166361870?previous_page=app_code_link
    videoId = re.search('https://www.douyin.com/video/(.*?)\?previous_page=app_code_link',PageUrl).group(1)
    
    JsonUrl = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={}".format(videoId)
    # print(JsonUrl)
    
    # jsonData = session.get(url=JsonUrl).text      # json形式的字典 <class 'str'>,将运行结果复制粘贴到 文件04_抖音无水印提取视频.json
    # jsonData = json.loads(jsonData)     # Python Dict 类型:<class 'dict'>
    # 等价于
    jsonData = session.get(url=JsonUrl).json()      # 将已编码的JSON字符串解码为Python对象,Python Dict 类型:<class 'dict'>
    
    # 在 04_抖音无水印提取视频.json 文件中Ctrl+F搜索视频Media路由的唯一id:v0300f670000c09rpp10r57bgg9sijlg
    videoUrl = jsonpath(jsonData,'$..video.play_addr.url_list')[0][0]
    # print(videoUrl)
    videoTitle = jsonpath(jsonData,'$..item_list..desc')[0]
    # print(videoTitle)
    videoUrl = videoUrl.replace("playwm","play")
    
    # 下载视频
    headers = {
        'user-agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) '
                     'Chrome/98.0.4758.102 Mobile Safari/537.36 '
    }
    responseVideo = session.get(url = videoUrl,headers=headers)
    with open('./video/'+videoTitle+'.mp4','wb') as f:
        f.write(responseVideo.content)
    print("----------{}下载完成--------------".format(videoTitle))
    

    文章到这里就结束了!希望大家能多多支持Python(系列)!六个月带大家学会Python,私聊我,可以问关于本文章的问题!以后每天都会发布新的文章,喜欢的点点关注!一个陪伴你学习Python的新青年!不管多忙都会更新下去,一起加油!

    Editor:Lonelyroots

    相关文章

      网友评论

          本文标题:Python(六十六)json模块、抖音视频无水印提取

          本文链接:https://www.haomeiwen.com/subject/ejvfrrtx.html