how to get redirect url using python requests
To get the resultant URL after you've been redirected, you can do r.url
.
r = requests.get('https://youtu.be/dQw4w9WgXcQ')
print(r.url) # https://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtu.be
r.history
is for URLs prior to the final one, so it's only returning your original URL because you were only redirected once.
How to get the final destination URL after redirections in python requests?
As suggested here: Python Requests library redirect new url
You can use the response history to get the final URL. In this case, the final URL will return a 200, however, it will have the "final final" redirect in the HTML. You can parse the final HTML to get the redirectURL.
I would use something like beautifulsoup4 to make parsing very easy - pip install beautifulsoup4
import requests
from bs4 import BeautifulSoup
from urllib.request import unquote
from html import unescape
doi_link = 'https://doi.org/10.1016/j.artint.2018.07.007'
response = requests.get(url= doi_link ,allow_redirects=True )
for resp in response.history:
print(resp.status_code, resp.url)
# use final response
# parse html and get final redirect url
soup = BeautifulSoup(response.text, 'html.parser')
redirect_url = soup.find(name="input" ,attrs={"name":"redirectURL"})["value"]
# get final response. unescape and unquote url from the HTML
final_url = unescape(unquote(redirect_url))
print(final_url)
article_resp = requests.get(final_url)
python requests library redirect new page to post request
Solved it using selenium webdriver and wait after login or any other GET request.
So that background request completes it's execution and return to the required page.
Whenever you face redirections while Sending any GET request, add SLEEP(seconds) method it can solve many problems
Related Topics
Python3 --Version Shows "Nameerror: Name 'Python3' Is Not Defined"
Nested Arguments Not Compiling
Main() Function Doesn't Run When Running Script
What Are the Arguments to Tkinter Variable Trace Method Callbacks
Display a Decimal in Scientific Notation
How to Fix Overlapping Annotations/Text
Access a Function Variable Outside the Function Without Using "Global"
Python: Why Is Functools.Partial Necessary
Python Selenium Wait for Several Elements to Load
Checking Multiple Values for a Variable
Multiprocessing.Pool: When to Use Apply, Apply_Async or Map
Pandas Read_Csv: Low_Memory and Dtype Options
Working with an Access Database in Python on Non-Windows Platform (Linux or MAC)
How to Get Text with Selenium Webdriver in Python
Python Strings and Integer Concatenation
Pandas Long to Wide Reshape, by Two Variables
How to Extract the Decision Rules from Scikit-Learn Decision-Tree