一、创建Scrapy工程
1 scrapy startproject 工程名
二、进入工程目录,根据爬虫模板生成爬虫文件
1 scrapy genspider -l # 查看可用模板2 scrapy genspider -t 模板名 爬虫文件名 允许的域名
三、定义爬取关注的数据(items.py文件)
四、编写爬虫文件
五、设置IP池或用户代理
(1)设置IP池
步骤1:在settings.py文件中添加代理服务器的IP信息,如:
1 # 设置IP池2 IPPOOL =[3 {"ipaddr": "221.230.72.165:80"},4 {"ipaddr": "175.154.50.162:8118"},5 {"ipaddr": "111.155.116.212:8123"}6 ]
步骤2:创建下载中间文件middlewares.py(与settings.py同一个目录),如:
#创建方法,cmd命令行,如项目为modetest,
E:\workspace\PyCharm\codeSpace\modetest\modetest>echo #middlewares.py
1 # -*- coding: utf-8 -*-
2 # 导入随机模块3 import random4 # 导入settings文件中的IPPOOL5 from.settings import IPPOOL6 # 导入官方文档对应的HttpProxyMiddleware7 fromscrapy.contrib.downloadermiddleware.httpproxy import HttpProxyMiddleware8
9 classIPPOOlS(HttpProxyMiddleware):10 # 初始化11 def __init__(self, ip=''):12 self.ip =ip13
14 # 请求处理15 def process_request(self, request, spider):16 # 先随机选择一个IP17 thisip =random.choice(IPPOOL)18 print("当前使用IP是:"+ thisip["ipaddr"])19 request.meta["proxy"] = "http://"+thisip["ipaddr"]
步骤3:在settings.py中配置下载中间件
1 # 配置下载中间件的连接信息2 DOWNLOADER_MIDDLEWARES ={3 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware':123,4 'modetest.middlewares.IPPOOlS' : 125
5 }
(2)设置用户代理
步骤1:在settings.py文件中添加用户代理池的信息(配置几个浏览器'User-Agent'),如:
1 # 设置用户代理池2 UPPOOL =[3 "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/0101 Firefox/52.0", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"
4 ]
步骤2:创建下载中间文件uamid.py(与settings.py同一个目录),如:
#创建方法,cmd命令行,如项目为modetest,
E:\workspace\PyCharm\codeSpace\modetest\modetest>echo #uamind.py
1 # -*- coding: utf-8 -*-#2 # 导入随机模块3 import random4 # 导入settings文件中的UPPOOL5 from.settings import UPPOOL6 # 导入官方文档对应的HttpProxyMiddleware7 fromscrapy.contrib.downloadermiddleware.useragent import UserAgentMiddleware8
9 classUamid(UserAgentMiddleware):10 # 初始化 注意一定要user_agent,不然容易报错11 def __init__(self, user_agent=''):12 self.user_agent =user_agent13
14 # 请求处理15 def process_request(self, request, spider):16 # 先随机选择一个用户代理17 thisua =random.choice(UPPOOL)18 print("当前使用User-Agent是:"+thisua)19 request.headers.setdefault('User-Agent',thisua)
步骤3:在settings.py中配置下载中间件
1 # 配置下载中间件的连接信息2 DOWNLOADER_MIDDLEWARES ={3 'scrapy.contrib.downloadermiddleware.useragent.UserAgentMiddleware': 2,4 'modetest.uamid.Uamid': 1
5 }
总而言之,有时候避免不了配置这类信息,所以直接在settings中都一起配置了如下,直接粘贴到settings.py文件的最后面
1 #========================================
2
3 #设置IP池和用户代理
4
5 #禁止本地Cookie
6 COOKIES_ENABLED =False7
8 #设置IP池
9 IPPOOL =[10 {"ipaddr": "221.230.72.165:80"},11 {"ipaddr": "175.154.50.162:8118"},12 {"ipaddr": "111.155.116.212:8123"}13 ]14
15 #设置用户代理池
16 UPPOOL =[17 "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/0101 Firefox/52.0", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"
18 ]19
20 #配置下载中间件的连接信息
21 DOWNLOADER_MIDDLEWARES ={22 #'scrapy.contrib.downloadermiddlewares.httpproxy.HttpProxyMiddleware':123,
23 #'modetest.middlewares.IPPOOlS' : 125,
24 'scrapy.contrib.downloadermiddleware.useragent.UserAgentMiddleware': 2,25 'modetest.uamid.Uamid': 1
26 }27
28 #============================================
如果觉得《pythonscrapy爬虫ip代理池_在Scrapy中使用IP池或用户代理(python3)》对你有帮助,请点赞、收藏,并留下你的观点哦!