Scrapy中添加隨機User-Agent

方法:使該模塊scrapy-fake-useragentpython

1,pip install scrapy-fake-useragent
dom

2,在setting.py 寫:scrapy

DOWNLOADER_MIDDLEWARES = {
    'lagoujob.middlewares.RandomUesrAgent': 1,
    'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware': None,
}

3,在 middlewares.py 寫入:ide

class RandomUesrAgent(object):
    def process_request(self, request, spider):
        ua = UserAgent()
        request.headers.setdefault("User-Agent", ua.random)

 或者:spa

而後就能夠用啦 blog

相關文章
相關標籤/搜索