M
M
Mark Adams2016-04-12 12:04:51
Python
Mark Adams, 2016-04-12 12:04:51

How to connect to different proxies via urllib.request.urlopen?

There is f-i:

def get_html(url): 
  try:
    response = urllib.request.urlopen(url).read()
  except:
    print('get error')
    response = '0'
  return response

In case of a connection error, you need to reconnect through another proxy. How to do it? And how do you set and call a proxy from the list?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2016-04-12
@ilyakmet

It can be like this for example:

import urllib2
import random

http_proxies = ['http://proxy1.com', 'http://proxy2.com', 'http://proxy3.com', ... ]

need_receive_data = True
while need_receive_data:
   try:
        random_proxy = random.choice(http_proxies)
        print 'Trying {proxy}'.format(proxy=random_proxy)
        proxy_handler = urllib2.ProxyHandler({'http': random_proxy })
        proxied = urllib2.build_opener(proxy_handler)
        proxied.open('http://какой-то сайт')
    except urllib2.HTTPError, e:
        print 'Error {err}'.format(err=str(e))
    else:
        need_receive_data = False
print proxied.read()

written hastily and not verified, but the meaning, I think, is clear

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question