zoukankan      html  css  js  c++  java
  • scrapy怎么设置带有密码的代理ip base64.encodestring不能用 python3.5,base64库里面的encodestring()被换成了什么?

    自己写爬虫时买的代理ip有密码,在网上查了都是下面这种:

    1、在Scrapy工程下新建"middlewares.py":

    import base64

    # Start your middleware class

    class ProxyMiddleware(object):

        # overwrite process request

        def process_request(self, request, spider):

            # Set the location of the proxy

            request.meta['proxy'] = "http://YOUR_PROXY_IP:PORT"

      

            # Use the following lines if your proxy requires authentication

            proxy_user_pass = "USERNAME:PASSWORD"

            # setup basic authentication for the proxy

            encoded_user_pass = base64.encodestring(proxy_user_pass)  #我用的python3.5,这个地方报错 TypeError: expected bytes-like object, not str

            request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass

    2、在项目配置文件里setting.py添加:

    DOWNLOADER_MIDDLEWARES = {

        ...   #此处省略其他中间件配置

        'ScrapyRedisTest.middlewares.ProxyMiddleware': 100,

    }

    ================================解决方法如下:(只改动标红的两行,其他的都不变)================================================

    import base64

    # Start your middleware class

    class ProxyMiddleware(object):

        # overwrite process request

        def process_request(self, request, spider):

            # Set the location of the proxy

            request.meta['proxy'] = "http://YOUR_PROXY_IP:PORT"

            # Use the following lines if your proxy requires authentication

            proxy_user_pass = "USERNAME:PASSWORD"

            # setup basic authentication for the proxy

            encoded_user_pass = base64.b64encode(proxy_user_pass.encode('utf-8')) 

          request.headers['Proxy-Authorization'] = 'Basic ' + str(encoded_user_pass, encoding="utf-8")

    2、在项目配置文件里setting.py添加:

    DOWNLOADER_MIDDLEWARES = {

        ...   #此处省略其他中间件配置

        'ScrapyRedisTest.middlewares.ProxyMiddleware': 100,

    }

  • 相关阅读:
    (四十九)Quartz2D自定义控件
    (四十八)Quartz2D引擎进阶
    (四十七)Quartz2D引擎初步
    (四十六)内存管理的复习
    (四十五)Modal 模态窗口 -遮盖
    (四十四)TabBarController和NagivationController配合
    (四十三)UITabBarController和AppDelegate的一些细节
    (四十二)tableView的滑动编辑和刷新 -局部刷新和删除刷新 -待解决问题
    linux命令——svn分支创建、合并
    C++异常处理
  • 原文地址:https://www.cnblogs.com/robinunix/p/8244526.html
Copyright © 2011-2022 走看看