【问题标题】:URL Redirect Not Recognized无法识别 URL 重定向
【发布时间】:2019-06-15 19:29:38
【问题描述】:

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

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

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。

【问题讨论】:

  • 您可能会遇到错误,因为服务器知道请求是从脚本发出的,而不是从真正的浏览器发出的。尝试检查响应的状态和正文

标签: python python-requests


【解决方案1】:

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

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"})

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    我可以下载视频,您只需请求 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)
    

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

    【讨论】:

      猜你喜欢
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 2016-10-22
      • 2013-05-08
      相关资源
      最近更新 更多