Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1495

How to get redirected url in Python?

$
0
0

I have a list of url to songs' tiktok page url in the form of f'https://www.tiktok.com/music/-{songid}'. If I enter it in my browser, I'll be redirected to a page with a longer url, which is what I want to get.

I was trying methods in other posts using requests and urllib.request, but neither works. What's happening? How could I get the redirected url in this case? Thank you!

For example, if I enter https://www.tiktok.com/music/-7224086788926638082 it in my browser, I'll be redirected to https://www.tiktok.com/music/UNFORGIVEN-feat-Nile-Rodgers-7224086788926638082, which is what I want to get. However, the following codes both return the original url, instead of the redirected one:

import requestsurl = 'https://www.tiktok.com/music/-7224086788926638082'r = requests.get(url, allow_redirects=True)print(r.url)
import urllib.requesturl = 'https://www.tiktok.com/music/-7224086788926638082'r = urllib.request.urlopen(url)print(r.geturl())

Viewing all articles
Browse latest Browse all 1495

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>