4
votes

I am coding a web scraper for the website with the following Python code:

import requests

def scrape(url):
    req = requests.get(url)
    with open('out.html', 'w') as f:
        f.write(req.text)

It works a few times but then an error HTML page is returned by the website (when I open my browser, I have a captcha to complete).

Is there a way to avoid this “ban” by for example changing the IP address?

2
IF the ban is based on your IP address, then yes changing IP address might resolve that, but that's not something python has control over. - Danielle M.
Well, to change the IP address you can run the exact same code on a different system with a different IP address... - Peteris
Have you consider, if you are getting banned and there's a captcha maybe the owners of the site doesn't want you to scrape their site? - MooingRawr
Why don't you try with a proxy? - t.m.adam

2 Answers

13
votes

As already mentioned in the comments and from yourself, changing the IP could help. To do this quite easily have a look at vpngate.py:

https://gist.github.com/Lazza/bbc15561b65c16db8ca8

An How to is provided at the link.

Have fun

6
votes

You can use a proxy with the requests library. You can find some free proxies at a couple different websites like https://www.sslproxies.org/ and http://free-proxy.cz/en/proxylist/country/US/https/uptime/level3 but not all of them work and they should not be trusted with sensitive information.

example:

proxy = {
    "https": 'https://158.177.252.170:3128',
    "http": 'https://158.177.252.170:3128' 
}
response=requests.get('https://httpbin.org/ip', proxies=proxy)