0

I'm trying to get redirected URL, but something doesn't work.

I tried two methods:

from urllib import request as uReq
import requests

#method 1
url_str = 'http://google.us/'
resp = req.urlopen(url_str)
print(resp.geturl())

#method 2
url_str = "http://google.us/"
resp = requests.get(url_str)
print(resp.url)

Both work and give result >>> https://www.google.com However, when I try to add this URL: http://www.kontrakt.szczecin.pl/lista-ofert/?f_listingId=351238&f=&submit=Szukaj as url_string nothing happens. When one's go to this site via browser he'll get that link: http://www.kontrakt.szczecin.pl/mieszkanie-wynajem-41m2-1850pln-janusza-kusocinskiego-centrum-szczecin-zachodniopomorskie,351238

It is important for me to get a link, because I need info from it.

Bart Bart
  • 11
  • 6
  • 2
    Well, a plain request to that URL returns 200, not 30x. In other words, that page does not redirect at an HTTP level. *Maybe* the server is detecting the user agent and chooses not to redirect non-browser agents; try spoofing your user agent. If that's not it, the redirect is done via Javascript or meta tag client-side, at which point it's up to you how you want to parse and emulate that… – deceze Jul 31 '18 at 10:20
  • Any ideas, I don't feel to advanced to make it on my own. But I'd like to learn it! – Bart Bart Jul 31 '18 at 13:03
  • Take a good hard look at that site's code to figure out what it's doing… – deceze Jul 31 '18 at 13:05
  • https://stackoverflow.com/questions/27652543/how-to-use-python-requests-to-fake-a-browser-visit. Maybe I try with this – Bart Bart Jul 31 '18 at 13:08

1 Answers1

-1

With allow_redirects=False you can make the url to stay on the page which you want even though it was intended to redirect.

resp = requests.get(url_str, allow_redirects=False)

You can find more such usage here

SanthoshSolomon
  • 1,383
  • 1
  • 14
  • 25