使用github的 scrapy-fake-useragent 不用自己改源码继承自带的userAgent中间件 只需要安装后增加配置即可
https://github.com/alecxe/scrapy-fake-useragent
pip install scrapy-fake-useragent
Configuration
Turn off the built-in UserAgentMiddleware
and add RandomUserAgentMiddleware
.
In Scrapy >=1.0:
DOWNLOADER_MIDDLEWARES = {
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware': None,
'scrapy_fake_useragent.middleware.RandomUserAgentMiddleware': 400,
}
In Scrapy <1.0:
DOWNLOADER_MIDDLEWARES = {
'scrapy.contrib.downloadermiddleware.useragent.UserAgentMiddleware': None,
'scrapy_fake_useragent.middleware.RandomUserAgentMiddleware': 400,
}