Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
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()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question