Search code examples
pythonurllib2

Changing the IP address for urllib2


I am trying to change the IP address from which my simple python code connects to my website.

import urllib.request  as urllib2
# change of IP address
page = urllib2.urlopen("http://example.com/").read()

Is there a python library that will easily enable it? So that the user who connects to the site displays different locations.

For example, I will want to scrape local news from the site using IP Address: 118.69.140.108 and port 53281.

How to do it, what library will enable it?


Solution

  • Try using the following code:

    import urllib.request  as urllib2
    
    proxy = urllib2.ProxyHandler({"http": "118.69.140.108:53281"})
    opener = urllib2.build_opener(proxy)
    urllib2.install_opener(opener)
    page = urllib2.urlopen("http://example.com/")
    

    Alternatively you can use the requests library which makes it easier:

    import requests
    
    url = "http://example.com/"
    page = requests.get(url, proxies={"http":"118.69.140.108:53281"})
    

    hope this helps