批量下载抖音某用户的所有作品

import requests, re, os, sys

def get_video(url):
    requests.packages.urllib3.disable_warnings()
    session = requests.session()

    headers = {'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
    res = session.get(url=url, headers=headers, verify=False)
    seu_udi = re.findall(r'sec_uid=(\w+-\w+-\w+|\w+-\w+|\w+)', res.url)

    sum_url = 'https://www.iesdouyin.com/web/api/v2/user/info/?sec_uid={0}'.format(seu_udi[0])
    se = session.get(sum_url).json()
    video_count = se['user_info']['aweme_count']
    nickname = se['user_info']['nickname']
    short_id = se['user_info']['short_id']
    print("nickname:%s" % nickname)
    print("video_count:%s" % video_count)
    f = open(f'{short_id}-{nickname}-{video_count}.txt', 'wt')
    f.close()

    max_cursor = 0
    id = 0
    w2 = 0
    while True:
        while True:
            if (max_cursor == 0):
                sec_id_url = "https://www.iesdouyin.com/web/api/v2/aweme/post/?sec_uid={0}&count=21&max_cursor=0&aid=1128&_signature=dF8skQAAK0iTKNSXi9av.XRfLI&dytk=".format(seu_udi[0])
            else:
                sec_id_url = "https://www.iesdouyin.com/web/api/v2/aweme/post/?sec_uid={0}&count=21&max_cursor={1}&aid=1128&_signature=dF8skQAAK0iTKNSXi9av.XRfLI&dytk=".format(seu_udi[0], max_cursor)
            sec_respone = session.get(url=sec_id_url, headers=headers, verify=False)
            comment = sec_respone.json()
            if (len(comment['aweme_list']) == 0):
                w2 += 1
                if w2 >= 1:
                    break 
                continue
            else:
                break

        max_cursor = comment['max_cursor']

        for s in comment['aweme_list']:
            id += 1

            desc = s['desc']
            text = s['aweme_id']
            dianzan = s['statistics']["digg_count"]
            pinglun = s['statistics']["comment_count"]
            fenxiang = s['statistics']["share_count"]
            video_url = s['video']['play_addr_lowbr']['url_list'][0]
            with open(f'{short_id}-{nickname}-{video_count}.txt', 'a') as f:
                 f.write(str(dianzan)+","+str(fenxiang)+","+video_url+"\n")
            print(str(id) +"/"+ str(video_count) + ",{0},点赞:{1},评论:{2},分享:{3} {4}".format(text, str(dianzan), str(pinglun),str(fenxiang), desc))

            # est = requests.get(url=video_url, headers=headers)
            # if not os.path.exists(short_id+'-'+nickname):
                # os.mkdir(short_id+'-'+nickname)
            # with open(f'{short_id}-{nickname}/{dianzan}-{text}.mp4', 'wb') as f:
                # f.write(est.content)

            if(int(id) >= int(video_count)):
                return

if __name__ == '__main__':
    if len(sys.argv)==1:
        print ('input url')
        sys.exit()
    url = sys.argv[1]
    get_video(url)

 

posted @ 2021-03-01 00:16  1CM  阅读(699)  评论(0编辑  收藏  举报