欢迎关注 『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爬虫』 专栏,持续更新中
『未完待续』