码迷,mamicode.com
首页 > 其他好文 > 详细

自定义代理IP

时间:2020-06-14 01:40:00      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:res   stp   配置文件   elf   random   随机   lib   red   spi   

自定义proxy.py
    import base64
    import random
    from urllib.parse import unquote, urlunparse
    from urllib.request import _parse_proxy

    from scrapy.utils.python import to_bytes


    class XdbProxyMiddleware:

        def _basic_auth_header(self, username, password):
            user_pass = to_bytes(
                %s:%s % (unquote(username), unquote(password)),
                encoding=latin-1)
            return base64.b64encode(user_pass)

        def process_request(self, request, spider):
            PROXIES = [
                        http://125.108.106.165:9000/,
                    ]
            url = random.choice(PROXIES)
            orig_type = ‘‘
            proxy_type, user, password, hostport = _parse_proxy(url)
            proxy_url = urlunparse((proxy_type or orig_type, hostport, ‘‘, ‘‘, ‘‘, ‘‘))

            if user:
                creds = self._basic_auth_header(user, password)
            else:
                creds = None
            # 给请求头里面换成随机代理IP
            request.meta[proxy] = proxy_url
            print(request.meta.get(proxy))
            if creds:
                # 加入代理用户认证
                request.headers[Proxy-Authorization] = bBasic  + creds
在配置文件中
    settings.py
        DOWNLOADER_MIDDLEWARES = {
           # ‘xdb.middlewares.XdbDownloaderMiddleware‘: 543,
           xdb.proxy.XdbProxyMiddleware: 751,
           # ‘scrapy.downloadermiddlewares.useragent.UserAgentMiddleware‘: None,
        }

 

自定义代理IP

标签:res   stp   配置文件   elf   random   随机   lib   red   spi   

原文地址:https://www.cnblogs.com/xiongfanyong/p/13122258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!