无法识别URL重定向

时间:2019-06-15 19:29:38

标签: python python-requests

我正在尝试使用视频ID link从Tik Tok下载视频

在浏览器中,以上链接将我重定向到以下URL 当我尝试使用request.get时,没有得到response.history,这意味着请求认为没有重定向。

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获取vid_url。

2 个答案:

答案 0 :(得分:3)

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

response = requests.get(post_url)

与此:

response = requests.get(post_url, headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:66.0) Gecko/20100101 Firefox/66.0", "Accept-Language": "en-US,en;q=0.5"})

希望这会有所帮助!

答案 1 :(得分:1)

我能够下载视频,只需请求vid_url,然后将视频写入文件即可。

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)

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