为什么需要settings
- setting用来存放一些公共的变量(比如:数据库,账号密码等)方便自己和别人修改。
- 一般用全大写字母命名变量名。 SQL_HOST=“192.168.0.1”
settings中一些配置的说明
BOT_NAME = 'yangguang'
NEWSPIDER_MODULE = 'yangguang.spiders'
USER_AGENT = 'yangguang (+http://www.yourdomain.com)'
ROBOTSTXT_OBEY = False
CONCURRENT_REQUESTS = 32
DOWNLOAD_DELAY = 3
CONCURRENT_REQUESTS_PER_DOMAIN = 16
CONCURRENT_REQUESTS_PER_IP = 16
COOKIES_ENABLED = False
DEFAULT_REQUEST_HEADERS = { ... }
SPIDER_MIDDLEWARES = {...}
DOWNLOADER_MIDDLEWARES = {...}
EXTENSIONS = {...}
ITEM_PIPELINES = {...}
我们还可以在settings中额外添加配置
- 在settings中添加:
MONGO_HOST = "localhost"
- 想在spiders中使用:
"""方式一 导入 万能方法"""
from yangguang.settings import MONGO_HOST
"""方式二 """
def parse(self, response):
self.settings["BOT_NAME"]
...
"""方式一 导入 万能方法"""
from yangguang.settings import MONGO_HOST
"""方式二 """
class YangguangPipeline(object):
def process_item(self, item, spider):
spider.settings["MONGO_HOST"]