Reputation: 131
I'm trying to download a video from TikTok using its video ID link.
In the browser, the above link redirects me to the full video URL.
When I try using requests.get
, I get no response.history
, meaning that requests
thinks that there was no redirect.
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")
This results in Request was not redirected
.
How can I get the redirect URL from the response
?
Upvotes: 1
Views: 917
Reputation: 1188
I am able to download the video all you have to do is to request for the vid_url and then write the video as a file.
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)
It's working i have checked. Hope you got what you were looking for
Upvotes: 0
Reputation: 4783
You should be using a header, try replacing this:
response = requests.get(post_url)
with this:
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"})
hope this helps!
Upvotes: 3