抖音
import requests import re url = 'https://www.douyin.com/video/7114220525978668303' headers = { 'cookie': 'douyin.com; ttcid=444dfe8e89ff4d99b0662076ad171c8775; ttwid=1%7CTnFKlrGi3lHjKf5bshFdP9Nwu_Vsiwo-TxvX9NISgj8%7C1642083887%7Cfbfa904ea2900763eb6ac090bdd09014d80840da1ca485bbfea193d5401b330e; MONITOR_WEB_ID=c27b9f4a-4917-4256-be93-e948308467e3; odin_tt=0510c3c4196f54b541a96ac64e8b585b3a755be85057da8a1f3fa068e3f7b75ca2de4345e2b856f1e7b3f9455d86079731fe7d07a9f10890f26855d3674858e1; passport_csrf_token=e0b90cb756903c370592bd558c2b0cf5; passport_csrf_token_default=e0b90cb756903c370592bd558c2b0cf5; s_v_web_id=verify_l268jj46_kc7yYkD6_YHWW_4x4v_9snI_EDE0zro77uRn; AVATAR_FULL_LOGIN_GUIDE_COUNT=1; AVATAR_FULL_LOGIN_GUIDE_TIMESTAMP=1650982839652; AVATAR_FULL_LOGIN_GUIDE_ITA_COUNT=1; AVATAR_FULL_LOGIN_GUIDE_ITA_TIMESTAMP=1650982839652; __ac_nonce=0627ba36600d465d72261; __ac_signature=_02B4Z6wo00f01zrB8EAAAIDCWcswKSh.eLM65fTAAKzW8srQpmSjmL6YX9IsdmMSL4a9EBuyJvIwNMROqFQktniG-Ur-UDPK6wHInC8QKqRYUmyGnflwUXLpKzPgVt2FtREyprGmCDAZLrIpcc; douyin.com; strategyABtestKey=1652269927.635; AB_LOGIN_GUIDE_TIMESTAMP=1652269927510; AVATAR_LOGIN_GUIDE_COUNT=1; _tea_utm_cache_2285=undefined; _tea_utm_cache_6383=undefined; _tea_utm_cache_1300=undefined; pwa_guide_count=3; IS_HIDE_THEME_CHANGE=1; THEME_STAY_TIME=299808; msToken=XGPVAVUHDi9iTEQRjdXuQ0YyetxhHq0c9EH1dLLpttanbCXsNSD0DRxwk9oUB0vZ7LB9vKd-ABi2kAkzj2lCn1x98lJ4iTFbf260RcLav-G4QkhNyq8qV9i3oEJRyc8t; home_can_add_dy_2_desktop=1; msToken=3ALqenaebbJHw7kQDiDG6aRAgVYm5WM1pVGqmyyidbGgYpWRWKn-wQ9tcjoxWrHvwcqoYAx3tQ4IGE1qixdq2ei_fPrirMeeI6HeooU3sGR2wyWQ2OAAh2RejVJOrmpA; tt_scid=Gp0q0JW0LDreTqplgpajIZNHCB0.p1NcVv0hhZBgaGDw4SFxkXGlXfKafiCVmWAWc537', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36' } response = requests.get(url=url, headers=headers) response.encoding = response.apparent_encoding #print(response.text) title = re.findall('<title data-react-helmet="true">(.*?)</title>', response.text)[0] href = re.findall('src(.*?)vRLmmJ', response.text)[0] video_url = requests.utils.unquote(href).replace('":"', 'https:') #print(video_url) video_content = requests.get(url=video_url).content with open('video\\' + title + '.mp4', mode='wb') as f: f.write(video_content) print(title, video_url)
虎牙
import requests import re for page in range(1, 5): print(f'正在采集第{page}页的数据内容') link = f'https://v.huya.com/g/all?set_id=51&order=hot&page={page}' headers = { '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=link, headers=headers) # print(response.text) # 从哪里<response.text里面>找什么数据<li data-vid="(\d+)"> \d+ 表示匹配一个或者多个数字 video_id_list = re.findall('<li data-vid="(\d+)">', response.text) print(video_id_list) for video_id in video_id_list: url = f'https://liveapi.huya.com/moment/getMomentContent?videoId={video_id}&uid=&_=1652789442223' response = requests.get(url=url, headers=headers) # print(response.json()['status']) title = response.json()['data']['moment']['title'] video_url = response.json()['data']['moment']['videoInfo']['definitions'][0]['url'] # 保存数据 >>> 也是需要发送请求, 获取数据 video_content = requests.get(url=video_url, headers=headers).content # 获取二进制数据 with open('video\\' + title + '.mp4', mode='wb') as f: f.write(video_content) print(title, '保存成功')