无法识别URL重定向

2024-09-30 16:39:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从Tik-Tok下载一段视频,使用它的视频ID link

在浏览器中,上面的链接将我重定向到下面的URL 当我尝试使用请求.get,我不明白响应历史记录,意味着请求认为没有重定向。在

import urllib.request
import requests

post_url = "https://api2.musical.ly/aweme/v1/play/?video_id=v09044a20000beeff4c108gs7sflfdug"
vid_url = "http://v16.muscdn.com/e8cee4f83f4c598a9d13ba6e4f7cead2/5d058940/video/tos/maliva/tos-maliva-v-0068/e5a1ab74d0b54f97b3578924a428e58d/?rc=amdvdnY7NDdpaDMzNTczM0ApQHRAbzg5ODozOjM0NDY0Ozg5PDNAKXUpQGczdSlAZjN2KUBmaGhkbGRlemhoZGY2NUByY2M0ZC1gY2JfLS1eMTZzczVvI28jQjItLzEuLi0tLS4uLi0uL2k6YjBwIzphLXEjOmAtbyNqdFxtK2IranQ6IzAuXg%3D%3D"

response = requests.get(post_url)
if response.history:
    print("Request was redirected")
    for resp in response.history:
        print(resp.status_code, resp.url)
    print("Final destination:")
    print(response.status_code, response.url)
else:
    print("Request was not redirected")

这将导致“请求未重定向”

我正在尝试从response获取视频url。在


Tags: importurlget视频responserequestvideopost
2条回答

您应该使用标题,请尝试替换此项:

response = requests.get(post_url)

有了这个:

^{pr2}$

希望这有帮助!在

我可以下载视频你所要做的就是请求视频的网址,然后把视频作为一个文件来写。在

import urllib.request
import requests

post_url = "https://api2.musical.ly/aweme/v1/play/?video_id=v09044a20000beeff4c108gs7sflfdug"
vid_url = "http://v16.muscdn.com/e8cee4f83f4c598a9d13ba6e4f7cead2/5d058940/video/tos/maliva/tos-maliva-v-0068/e5a1ab74d0b54f97b3578924a428e58d/?rc=amdvdnY7NDdpaDMzNTczM0ApQHRAbzg5ODozOjM0NDY0Ozg5PDNAKXUpQGczdSlAZjN2KUBmaGhkbGRlemhoZGY2NUByY2M0ZC1gY2JfLS1eMTZzczVvI28jQjItLzEuLi0tLS4uLi0uL2k6YjBwIzphLXEjOmAtbyNqdFxtK2IranQ6IzAuXg%3D%3D"

response = requests.get(vid_url)

with open("python_logo.mp4",'wb') as f:

    # Saving received content as a mp4 file in
    # binary format

    # write the contents of the response
    # to a new file in binary mode.
    f.write(response.content)

我已经检查过了。希望你得到了你想要的

相关问题 更多 >