Access web site by location in python

243 views Asked by At

Some web site have a pt., en. in the beginner or .br, .it at the end because of the server location.

When I use the library of python as the function urlopen I have to pass the full adress string of the web site, including the termination string of the server location (for international servers).

Some international web sites have the each country service. There some way to python make this transparent to the user? (adding the termination or starter string) Because some webpage to not redirect to the local proximity server in an automatic way.

1

There are 1 answers

0
Taku On BEST ANSWER

If you try to access google.com and google decides to forward you automatically to google.se (for example), there's nothing the client can do about it - whether that client is a human or a python script. That is controlled by the webserver, not the client.

What Danielle said in the comment is not entirely correct, when the client access the webpage "google.com", the site host noticed your ip location and send back a signal telling the browser to redirect the current site to "google.se" (To go with Danielle's example) to make the site match your ip location. However, you can avoid redirects. As for the sake of the question, here's a simple demonstration using python Requests library. Setting allow_redirects to False.

import requests

r = requests.get('https://www.google.com')
print(r.url)
# 'https://www.google.ca/?gfe_rd=cr&dcr=0&ei=mpewWZGdGePs8we597n4Dw'
# requests automatically followed the redirect link to google.ca

r = requests.get('https://www.google.com', allow_redirects=False)
print(r.url)

# 'https://www.google.com/'
# here it says at google.com

Your question isn't clear enough to provide a more thorough answer. But I hope my example has helped you a bit.