# -*- coding: utf-8 -*- # Scrapy settings for lizi project # # For simplicity, this file contains only settings considered important or # commonly used. You can find more settings consulting the documentation: # # http://doc.scrapy.org/en/latest/topics/settings.html # http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html # http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html BOT_NAME = 'lizi' SPIDER_MODULES = ['lizi.spiders'] NEWSPIDER_MODULE = 'lizi.spiders' # Crawl responsibly by identifying yourself (and your website) on the user-agent USER_AGENT = 'lizi (+http://www.yourdomain.com)' '''默認: "Scrapy/VERSION (+http://scrapy.org)" 爬取的默認User-Agent,除非被覆蓋。''' # Obey robots.txt rules #ROBOTSTXT_OBEY = True ROBOTSTXT_OBEY = False '如果啟用,Scrapy將會尊重 robots.txt策略' CONCURRENT_REQUESTS = 32 '''*********************** Configure maximum concurrent requests performed by Scrapy (default: 16) Scrapy downloader 並發請求(concurrent requests)的最大值 ''' # Configure a delay for requests for the same website (default: 0) # See http://scrapy.readthedocs.org/en/latest/topics/settings.html#download-delay # See also autothrottle settings and docs DOWNLOAD_DELAY = 3 '''************************ 下載器在下載同一個網站下一個頁面前需要等待的時間。 該選項可以用來限制爬取速度, 減輕服務器壓力。同時也支持小數: 該設定影響(默認啟用的) RANDOMIZE_DOWNLOAD_DELAY 設定。 默認情況下,Scrapy在兩個請求間不等待一個固定的值, 而是使用0.5到1.5之間的一個隨機值 * DOWNLOAD_DELAY 的結果作為等待間隔。 當 CONCURRENT_REQUESTS_PER_IP 非0時,延遲針對的是每個ip而不是網站。 另外您可以通過spider的 download_delay 屬性為每個spider設置該設定。 ''' # The download delay setting will honor only one of: CONCURRENT_REQUESTS_PER_DOMAIN = 16 '對單個網站進行並發請求的最大值' CONCURRENT_REQUESTS_PER_IP = 16 ''' 對單個IP進行並發請求的最大值。如果非0,則忽略 CONCURRENT_REQUESTS_PER_DOMAIN 設定, 使用該設定。 也就是說,並發限制將針對IP,而不是網站。 該設定也影響 DOWNLOAD_DELAY: 如果 CONCURRENT_REQUESTS_PER_IP 非0, 下載延遲應用在IP而不是網站上。 ''' # Disable cookies (enabled by default) COOKIES_ENABLED = False '是否啟用cookies middleware。如果關閉,cookies將不會發送給web server。' # Disable Telnet Console (enabled by default) TELNETCONSOLE_ENABLED = False '表明 telnet 終端 (及其插件)是否啟用的布爾值。' # Override the default request headers: DEFAULT_REQUEST_HEADERS = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'en',} 'Scrapy HTTP Request使用的默認header。由 DefaultHeadersMiddleware 產生。這里通常可以自己添加更完整' # Enable or disable spider middlewares # See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html SPIDER_MIDDLEWARES = { 'lizi.middlewares.MyCustomSpiderMiddleware': 543, } '''要啟用spider中間件,您可以將其加入到 SPIDER_MIDDLEWARES 設置中。 該設置是一個字典,鍵位中間件的路徑,值為中間件的順序(order)。如上就是開啟''' # Enable or disable downloader middlewares # See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html DOWNLOADER_MIDDLEWARES = { 'lizi.middlewares.MyCustomDownloaderMiddleware': 543, } '''保存項目中啟用的下載中間件及其順序的字典。 更多內容請查看 激活下載器中間件 DOWNLOADER_MIDDLEWARES 設置會與Scrapy定義的 DOWNLOADER_MIDDLEWARES_BASE 設置合並(但不是覆蓋), 而后根據順序(order)進行排序, 最后得到啟用中間件的有序列表: 第一個中間件是最靠近引擎的,最后一個中間件是最靠近下載器的。 關於如何分配中間件的順序請查看 DOWNLOADER_MIDDLEWARES_BASE 設置, 而后根據您想要放置中間件的位置選擇一個值。 由於每個中間件執行不同的動作,您的中間件可能會依賴於之前(或者之后)執行的中間件, 因此順序是很重要的''' # Enable or disable extensions # See http://scrapy.readthedocs.org/en/latest/topics/extensions.html EXTENSIONS = { 'scrapy.extensions.telnet.TelnetConsole': None, } # Configure item pipelines # See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html ITEM_PIPELINES = { 'lizi.pipelines.SomePipeline': 300, } #這里如果一個項目多個spiders的時候,每次運行的時候每次要在這里制定一個對應的pipeline ''' 保存項目中啟用的pipeline及其順序的字典。該字典默認為空,值(value)任意。 不過值(value)習慣設定在0-1000范圍內。 為了兼容性,ITEM_PIPELINES 支持列表,不過已經被廢棄了。 樣例: ITEM_PIPELINES = { 'mybot.pipelines.validate.ValidateMyItem': 300, 'mybot.pipelines.validate.StoreMyItem': 800, }''' # Enable and configure the AutoThrottle extension (disabled by default) # See http://doc.scrapy.org/en/latest/topics/autothrottle.html AUTOTHROTTLE_ENABLED = True '啟用自動限速AutoThrottle擴展' # The initial download delay AUTOTHROTTLE_START_DELAY = 5 '初始下載延遲(單位:秒)' # The maximum download delay to be set in case of high latencies AUTOTHROTTLE_MAX_DELAY = 60 '初始下載延遲(單位:秒)' # The average number of requests Scrapy should be sending in parallel to # each remote server AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0 # Enable showing throttling stats for every response received: AUTOTHROTTLE_DEBUG = False '起用AutoThrottle調試(debug)模式,展示每個接收到的response。 您可以通過此來查看限速參數是如何實時被調整的' # Enable and configure HTTP caching (disabled by default) # See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings HTTPCACHE_ENABLED = True 'HTTP緩存是否開啟。' HTTPCACHE_EXPIRATION_SECS = 0 '''緩存的request的超時時間,單位秒。 超過這個時間的緩存request將會被重新下載。如果為0,則緩存的request將永遠不會超時。''' HTTPCACHE_DIR = 'httpcache' '''存儲(底層的)HTTP緩存的目錄。如果為空,則HTTP緩存將會被關閉。 如果為相對目錄,則相對於項目數據目錄(project data dir)。 更多內容請參考 默認的Scrapy項目結構 。''' HTTPCACHE_IGNORE_HTTP_CODES = [] '''不緩存設置中的HTTP返回值(code)的request。''' HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage' '實現緩存存儲后端的類。' #一下內容為默認setting.py文件沒有的字段,但是可以自己添加 CONCURRENT_ITEMS ''' 默認: 100 Item Processor(即 Item Pipeline) 同時處理(每個response的)item的最大值。 ''' DEFAULT_ITEM_CLASS '''默認: 'scrapy.item.Item' the Scrapy shell 中實例化item使用的默認類''' DEPTH_LIMIT '''默認: 0 爬取網站最大允許的深度(depth)值。如果為0,則沒有限制''' DEPTH_PRIORITY '''默認: 0 整數值。用於根據深度調整request優先級。 如果為0,則不根據深度進行優先級調整''' DEPTH_STATS ''' 默認: True 是否收集最大深度數據。''' DEPTH_STATS_VERBOSE '''默認: False 是否收集詳細的深度數據。如果啟用,每個深度的請求數將會被收集在數據中。''' DOWNLOAD_HANDLERS ''' 默認: {} 保存項目中啟用的下載處理器(request downloader handler)的字典。 例子請查看 DOWNLOAD_HANDLERS_BASE 。 DOWNLOAD_HANDLERS_BASE 默認: { 'file': 'scrapy.core.downloader.handlers.file.FileDownloadHandler', 'http': 'scrapy.core.downloader.handlers.http.HttpDownloadHandler', 'https': 'scrapy.core.downloader.handlers.http.HttpDownloadHandler', 's3': 'scrapy.core.downloader.handlers.s3.S3DownloadHandler', } 保存項目中默認啟用的下載處理器(request downloader handler)的字典。 永遠不要在項目中修改該設定,而是修改 DOWNLOADER_HANDLERS 。 如果需要關閉上面的下載處理器,您必須在項目中的 DOWNLOAD_HANDLERS 設定中設置該處理器,並為其賦值為 None 。 例如,關閉文件下載處理器: DOWNLOAD_HANDLERS = { 'file': None, }''' DOWNLOAD_TIMEOUT ''' 默認: 180 下載器超時時間(單位: 秒)。''' LOG ''' LOG_ENABLED 默認: True 是否啟用logging。 LOG_ENCODING 默認: 'utf-8' logging使用的編碼。 LOG_FILE 默認: None logging輸出的文件名。如果為None,則使用標准錯誤輸出(standard error)。 LOG_LEVEL 默認: 'DEBUG' log的最低級別。可選的級別有: CRITICAL、 ERROR、WARNING、INFO、DEBUG。更多內容請查看 Logging 。 LOG_STDOUT 默認: False 如果為 True ,進程所有的標准輸出(及錯誤)將會被重定向到log中。例如, 執行 print 'hello' ,其將會在Scrapy log中顯示。''' RANDOMIZE_DOWNLOAD_DELAY ''' 默認: True 如果啟用,當從相同的網站獲取數據時,Scrapy將會等待一個隨機的值 (0.5到1.5之間的一個隨機值 * DOWNLOAD_DELAY)。 該隨機值降低了crawler被檢測到(接着被block)的機會。某些網站會分析請求, 查找請求之間時間的相似性。 隨機的策略與 wget --random-wait 選項的策略相同。 若 DOWNLOAD_DELAY 為0(默認值),該選項將不起作用 '''