账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    scapy爬虫怎样检测配置的IP代理是否生效?
    81
    0

    您好!

    问题:

    1. 通过response.request.headers() 能检测到当前请求的header信息(因为随机了user-agent),但是想确定配置的IP代理 是否生效应该怎么获取当前请求用的哪个ip?
    2. 一般来说,更换了user-agent,和ip地址,会出现验证码的网页就不会出现了,对吗?如果是带了cookie的原因,我不用cookie 依然出现验证码 让我的爬虫结束运行,所以怀疑IP代理没有配置上。

      # 代理接口 的中间件
      class ProxyAPIMiddleware(object):

         def process_request(self, request, spider):
             req = urllib.request.Request('请求的ip接口url')
             response = urllib.request.urlopen(req)
             ip = 'http://%s' % str(response.read(), 'utf-8')   #获取到ip+端口 
             request.meta['proxy'] = ip                         #配置ip
             print(request.meta['proxy'])                       #运行的时候 这里是有输出的,说明成功调用了代理API,但是request.meta['proxy'] = ip 使是设置当前请求的ip的吧?
      

    运行的时候:

         .
         .
         .
         .
        2018-06-23 15:57:29 [scrapy.middleware] INFO: Enabled spider middlewares:
        ['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
         'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
         'scrapy.spidermiddlewares.referer.RefererMiddleware',
         'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
         'scrapy.spidermiddlewares.depth.DepthMiddleware']
        链接数据库=================
        2018-06-23 15:57:30 [scrapy.middleware] INFO: Enabled item pipelines:
        ['soopat_patent.pipelines.SoopatPatentPipeline']
        2018-06-23 15:57:30 [scrapy.core.engine] INFO: Spider opened
        2018-06-23 15:57:30 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
        2018-06-23 15:57:30 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
        更换User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
        请求代理
        设置请求ip: http://122.230.248.127:4523
        
        http://122.230.248.127:4523
        
        2018-06-23 15:57:33 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET http://www.soopat.com/>; (failed 1 times): [<twisted.python.failure.Failure twisted.internet.error.ConnectionLost: Connection to the other side was lost in a non-clean fashion.>]
        更换User-Agent: Mozilla/5.0 (compatible; WOW64; MSIE 10.0; Windows NT 6.2)
        请求代理
        设置请求ip: http://60.172.68.112:4507
        
        http://60.172.68.112:4507
        2018-06-23 15:57:39 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://www.soopat.com/>; (referer: http://www.soopat.com/)
        正在检索分析相关的结果=========================
        更换User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10
        请求代理
        设置请求ip: http://140.255.4.142:4523
        
        http://140.255.4.142:4523
        
        2018-06-23 15:57:43 [scrapy.core.engine] DEBUG: Crawled (200) <GET ....)
        
        验证码缓冲时间。。
        有验证码,等待一会儿在试...
        当前页所有类别列表[]
        没有进入详情页,可能出现验证码。。。
        list index out of range
        2018-06-23 15:57:48 [scrapy.core.engine] INFO: Closing spider (finished)
        2018-06-23 15:57:48 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
        
        .
        .
        .
        
        

    scrapy爬虫 在刚运行的时候是正常的,数据正常入库,但是第二天再次运行的时候 ,直接被验证码挡住了。
    爬虫小白,虚心请教,谢谢各位了。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部