0

我有一个从 TikTok 获取无水印视频网址的功能,但它不再起作用了。

如果我有视频 ID,如何获取 TikTok 无水印视频网址?

def get_tiktok_video_nowatermark(url):
   headers = {
       "method": "GET",
       "accept-encoding": "utf-8",
       "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
   }
   req = requests.get(url, headers=headers).text

   video_data = json.loads(re.findall(u"<script id=\"__NEXT_DATA__\" type=\"application/json\" crossorigin=\"anonymous\">(.*?)</script><script crossorigin=\"anonymous\" nomodule=", req)[0])

   watermark_url = video_data["props"]["pageProps"]["videoData"]["itemInfos"]["video"]["urls"][0]

   watermark_video = str(requests.get(watermark_url, headers=headers).content)

   position = int(re.search(r"vid:", watermark_video).start())

   nowatermark_url = "https://api2.musical.ly/aweme/v1/playwm/?video_id=" + watermark_video[position+4:position+36]

   return nowatermark_url
4

1 回答 1

2
def get_tiktok_video_nowatermark(url):
   headers = {
       "method": "GET",
       "accept-encoding": "utf-8",
       "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
   }
   req = requests.get(url, headers=headers).text

   video_data = json.loads(re.findall(u"<script id=\"__NEXT_DATA__\" type=\"application/json\" crossorigin=\"anonymous\">(.*?)</script><script crossorigin=\"anonymous\" nomodule=", req)[0])

   watermark_url = video_data["props"]["pageProps"]["videoData"]["itemInfos"]["video"]["urls"][0]

   watermark_video = str(requests.get(watermark_url, headers=headers).content)

   position = int(re.search(r"vid:", watermark_video).start())

   nowatermark_url = "https://api.tiktokv.com/aweme/v1/play/?video_id=" + watermark_video[position+4:position+36] + "&vr_type=0&is_play_url=1&source=PackSourceEnum_PUBLISH&media_type=4&ratio=default&improve_bitrate=1"

   return nowatermark_url

另外,请确保您的请求nonwatermark_url没有任何用户代理标头,否则您将得到空结果。如果您可以向 url 发出 head 请求并确定final url哪些可以直接传递给浏览器进行播放或下载,那就更好了。

于 2020-09-04T09:51:21.137 回答