Reputation: 120
I want to open a link from a website using Python, so here is the flow:
I open the main URL (e.g. www.url1.com)
I scrape the page and find the button, it has a redirection link (www.url2.com)
when I use this link in browser it redirects to (www.url3.com) then immediately goes to another (Required link) (www.url4.com)
When I try the same flow using Python requests, it only goes to (www.url3.com)
I tried using the allow_redirects
argument without any success
Here is my code:
import requests
headers = {
'User-Agent': '',
'authority': '',
'scheme': '',
'accept': '',
'x-requested-with': '',
'cookie': '',
'referer':
}
def download(req):
resp = requests.get(req, headers=headers, allow_redirects=True)
print(resp.text)
I also tried to print history using this answer.
but it keeps redirecting me too (url3)
Upvotes: 1
Views: 516
Reputation: 4783
It's quite difficult to give a full answer without having the actual URLs you are using. That being said I think the problem is due to the fact that you are not saving/keeping track of the cookies, for that I would recommend you using requests.session()
when sending requests as it keeps track of the cookies for you.
All in all, I would recommend trying the following code:
import requests
session = requests.session()
headers = {
'User-Agent': '',
'authority': '',
'scheme': '',
'accept': '',
'x-requested-with': '',
'cookie': '',
'referer':
}
def download(req):
global session
resp = session.get(req, headers=headers, allow_redirects=True)
print(resp.text)
(PS: if you are scraping a website I would highly recommend you use a User-Agent in the headers instead of leaving it blank)
Hope this helps
Upvotes: 1