ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
安装fake-useragent模块 ~~~ pip install fake-useragent ~~~ 在middlewares.py文件中导入模块 ~~~ from fake_useragent import UserAgent ~~~ 定义UAMiddleware中间件 ~~~ class UAMiddleware(object): def __init__(self, crawler): super(UAMiddleware, self).__init__() self.ua = UserAgent() @classmethod def from_crawler(cls, crawler): return cls(crawler) def process_request(self, request, spider): request.headers.setdefault('User-Agent', self.ua.random) ~~~ 在settings.py配置文件DOWNLOADER_MIDDLEWARES中添加该中间件