Python 无法识别URL重定向
我正在尝试使用Tik Tok的视频ID从它下载视频 在浏览器中,上面的链接将我重定向到以下位置 当我尝试使用requests.get时,没有得到response.history,这意味着requests认为没有重定向Python 无法识别URL重定向,python,python-requests,Python,Python Requests,我正在尝试使用Tik Tok的视频ID从它下载视频 在浏览器中,上面的链接将我重定向到以下位置 当我尝试使用requests.get时,没有得到response.history,这意味着requests认为没有重定向 import urllib.request import requests post_url = "https://api2.musical.ly/aweme/v1/play/?video_id=v09044a20000beeff4c108gs7sflfdug" 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(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")
这导致请求未重定向
我正在尝试从响应中获取vid_url。您应该使用标题,请尝试替换此标题:
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"})
希望这有帮助 您应该使用标题,请尝试替换此标题:
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"})
希望这有帮助 我可以下载视频,您只需请求视频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)
我检查过了,它正在工作。希望你得到了你想要的我可以下载视频,你所要做的就是请求视频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)
我检查过了,它正在工作。希望您得到了所需的内容您可能会遇到错误,因为服务器知道请求是从脚本发出的,而不是从真正的浏览器发出的。尝试检查响应的状态和主体。如果您收到错误,因为服务器知道请求是从脚本发出的,而不是从真正的浏览器发出的。尝试检查响应的状态和主体