3

I know this question is asked here Scrapy ERROR: Error downloading - Could not open CONNECT tunnel

But I don't find solution and since I don't have enough reputation I can't comment there and seek for answer.

So here is my settings.py file code

 DOWNLOADER_MIDDLEWARES = {
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware': 110,
'clientemails.middlewares.ProxyMiddleware': 100,
}   


PROXIES = [{'ip_port': '74.118.91.233:8080', 'user_pass': ''},
           {'ip_port': '83.222.222.137:8080', 'user_pass': ''},
           {'ip_port': '197.89.196.48:8080', 'user_pass': ''},
           {'ip_port': '112.96.29.187:80', 'user_pass': ''},
           {'ip_port': '202.194.101.150:80', 'user_pass': ''},
           {'ip_port': '213.85.92.10:80', 'user_pass': ''},
           {'ip_port': '221.208.194.108:80', 'user_pass': ''},
           {'ip_port': '190.63.140.71:80', 'user_pass': ''},
           {'ip_port': '223.19.196.232:80', 'user_pass': ''},
           {'ip_port': '195.71.127.224:80', 'user_pass': ''},]

And middlewares.py have this code

import base64
import random
from settings import PROXIES

class ProxyMiddleware(object):
    def process_request(self, request, spider):
        proxy = random.choice(PROXIES)
        if proxy['user_pass'] is not None:
            request.meta['proxy'] = "http://%s" % proxy['ip_port']
            encoded_user_pass = base64.encodestring(proxy['user_pass'])
            request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass            
        else:
            request.meta['proxy'] = "http://%s" % proxy['ip_port']

When I run the bot I get this error:

 Could not open CONNECT tunnel.

Any help would be appreciated.

Community
  • 1
  • 1
user2728494
  • 131
  • 1
  • 10

1 Answers1

1

Instead of passing your proxy authorization in the headers, try formatting your requests like this instead:

request.meta['proxy'] = "http://{}:{}@{}:{}".format(user,pass,'127.0.0.1','8118')
Benjamin James
  • 941
  • 1
  • 9
  • 24