I want to be able to get the username of a Twitter user using their Twitter user id without using the Twitter API.
I am trying to do this by getting the redirect URL from the requests module (and getting the username from the URL); however, it has not been working.
This is what I have tried:
import requests
response = requests.get('https://twitter.com/i/user/783214')
url = response.url
# should print https://twitter.com/Twitter but prints https://twitter.com/i/user/783214
print(url)
Why is the proper redirected URL not being returned here? Thanks!
CodePudding user response:
It's because Twitter does not use an HTTP redirect.
It probably uses a Javascript form of redirection like changing window.location.href
, requests is not a browser so it does not execute the javascript in the page you requested hence the absence of redirection.