• scrapy download delay, CONCURRENT_REQUESTS


    设置delay有起码两个好处, 一个是对被爬对象表示礼貌, 另一个是爬的太快,很多服务器会封ip,或限制访问。

    效果:每x秒左右来一个request

    先建立一个项目来找CONCURRENT_REQUESTS与DOWNLOAD_DELAY的联系

    大致给出粗略代码:

    jianshuspider.py:


    import scrapy
    from JianshuSpider_author_1.items

    import JianshuspiderAuthor1Item

    from scrapy.selector
    import Selector

    class JianshuSpider(scrapy.Spider):

    name ="jianshu"

    def start_requests(self):

    urls = ['http://www.jianshu.com/users/958f740aed52/followers']

    for url
    in urls:

    yield scrapy.Request(
    url = url,
    callback=
    self.parse_author)

    def parse_author(

    self,

    response):
    item = JianshuspiderAuthor1Item()

    selector = Selector(response)

    fans_href = selector.xpath("//div[@class='info']/a/@href").extract()

    for fan_href
    in fans_href:

    fan_href ='http://www.jianshu.com/users/'+ fan_href.split('/')[-1] +'/followers'

    # fan_href = 'http://www.google.com.hk/'+ fan_href.split('/')[-1] + '/followers'#需要timeout时调用

    yield scrapy.Request(fan_href,
    callback=self.parse_author)


    item['author'] = selector.xpath("//div[@class='title']/a/text()").extract_first()

    yield item

    requestlimit.py(downlomiddleware):


    class RequestLimitMiddleware(object):

    count =0

    def process_request(self,request,spider):

    self.count +=1

    print(self.count)

    以上两个文件的代码为核心代码。


    测试结果:

    一:

    settings.py

    CONCURRENT_REQUESTS =8

    DOWNLOAD_DELAY =0


    并且jianshuspider.py中关闭递归简书链接,打开Google链接语句

    效果:8个request同时来,同时timeout。8个request又来,又timeout。如此循环。

    二:

    settings.py

    CONCURRENT_REQUESTS =1

    DOWNLOAD_DELAY =5

    并且jianshuspider.py中打开递归简书链接,关闭Google链接语句

    效果:每5秒左右来一个request

    三:

    settings.py

    CONCURRENT_REQUESTS =2
    DOWNLOAD_DELAY =5

    并且jianshuspider.py中打开递归简书链接,关闭Google链接语句

    效果:一开始来2个request(A,B),但5秒后只处理了一个request(A),新来一个request(C),5秒后又处理一个request(B),排队一个request(D)。如此循环。

    总结:

    DOWNLOAD_DELAY 会影响 CONCURRENT_REQUESTS,不能使并发显现出来。

    思考:

    当有CONCURRENT_REQUESTS,没有DOWNLOAD_DELAY 时,服务器会在同一时间收到大量的请求。

    当有CONCURRENT_REQUESTS,有DOWNLOAD_DELAY 时,服务器不会在同一时间收到大量的请求。

  • 相关阅读:
    Oracle:DBMS_STATS.GATHER_TABLE_STATS的语法
    Oracle Purge和drop的区别
    Oracle Pipelined Table Functions简介
    Firefox与IE浏览器缓存的两个重要区别
    @SuppressWarnings的使用、作用、用法
    正确优雅地解决用户退出——JSP及Struts解决方案
    log4j详解与实战
    maven项目在eclipse中debug时看不到源码?
    maven tomcat eclipse 配置 debug
    Java泛型详解
  • 原文地址:https://www.cnblogs.com/andy0816/p/15257299.html
Copyright © 2020-2023  润新知