gpt-3.5-turbo-0125· OpenAI
WildChat-4.8M (AI2)
Follow a redirect on python requests
- User
Follow a redirect on python requests
- Assistant
To follow a redirect using the
requestslibrary in Python, you can set theallow_redirectsparameter toTruewhen making a request. This will allowrequeststo automatically follow any redirect responses.Here's an example:
import requests # Make a GET request with allow_redirects set to True response = requests.get('http://example.com', allow_redirects=True) # Check if the request was successful if response.ok: print('Request was successful') # Get the final URL after following redirects final_url = response.url print('Final URL:', final_url)In the above example, the
requests.getfunction is used to send a GET request to a URL withallow_redirects=True. Theresponse.urlproperty will contain the final URL after all redirects have been followed.