欢迎关注 『scrapy爬虫』 专栏,持续更新中
欢迎关注 『scrapy爬虫』 专栏,持续更新中

start_requests中添加代理

pipelines.py通道中对应的通道的start_requests

    def start_requests(self) :
        for page in range(10): #10页
            yield Request(
                url=f'https://movie.douban.com/top250?start={page*25}&filter=',
                meta={'proxy':"socket5://127.0.0.1:1086"},#socket5代理
                # meta={'proxy':"http://127.0.0.1:1086"}#购买的商业代理一般是http给一个api接口
            )

中间件中添加代理

middlewares.py中的MyscrapyDownloaderMiddleware下面的process_request函数

class MyscrapyDownloaderMiddleware:
	#------省略各种函数--------
    def process_request(self, request, spider):
        # Called for each request that goes through the downloader
        # middleware.

        # Must either:
        # - return None: continue processing this request
        # - or return a Response object
        # - or return a Request object
        # - or raise IgnoreRequest: process_exception() methods of
        #   installed downloader middleware will be called
        request.meta={'proxy':"socket5://127.0.0.1:1086"}#在中间件中请求前拦截请求 添加代理
        return None

总结

大家喜欢的话,给个👍,点个关注!给大家分享更多计算机专业学生的求学之路!

版权声明:

发现你走远了@mzh原创作品,转载必须标注原文链接

Copyright 2024 mzh

Crated:2024-3-1

欢迎关注 『scrapy爬虫』 专栏,持续更新中
欢迎关注 『scrapy爬虫』 专栏,持续更新中
『未完待续』


Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐