4

Python中Scrapy框架的代理使用

 1 year ago
source link: https://blog.51cto.com/u_15811883/5737745
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python中Scrapy框架的代理使用

精选 原创

IPIDEA01 2022-10-08 14:41:06 ©著作权

文章标签 ide html xml 文章分类 Python 编程语言 阅读数248

本文中所涉及的网站皆以GG代替。

scrapy框架,熟悉python爬虫的朋友们应该知道甚至有所了解,scrapy是一个爬虫框架,模块化程度高,可拓展性强,对相应的模块进行开发和拓展就能满足使用者想要得到的效果。

所以本次我就简单介绍下scrapy的使用和代理的配置。

一、scrapy的配置

说是配置,其实scrapy也没啥可配置的,因为他新建项目真的很简单,再你想要创建项目的目录下输入或者打开cmd输入:

scrapy startproject mySpider(你想要的项目名)

但是前提是,你得需要pip安装scrapy:

pip install scrapy

当你pip安装好scrapy并且部署好scrapy项目之后,最基础的东西就搭建好了。

二、scrapy的使用

还是先看一下scrapy的目录结构吧家人们。

首先我先创建了一个叫做 scrapytest 的项目,目录结构如下:

Python中Scrapy框架的代理使用_xml

Python中Scrapy框架的代理使用_html_02

从外到内,scrapy.cfg 是scrapy项目的配置文件。items.py定义数据结构,规定了你想存什么样的数据和数据类型。pipelines.py 正如其名管道文件,产生的数据类型再次做数据处理。setting.py 全局设置。 middlewares.py 中间件。spiders 自定爬虫规则。

首先创建一个spider:

scrapy genspider GGSpider GG.com

输入指令后会自动帮你创建一个spider文件:

Python中Scrapy框架的代理使用_xml_03

查看里面的元素,name代表此项目的名字,allowed_domains 标识允许爬取的域名并过滤非此域名的链接,start_urls 待爬取url。

修改一下start_urls里面的链接,把http改成https后,新建一个方法,开始进行爬取:

1. def start_requests(self):
2. for url in self.starturl:
3. GGHeaders = {
4. "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
5. "authority": "www.GG.com",
6. "user-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.66 Safari/537.36",
7. }
8. yield scrapy.Request(url,headers=googleHeaders)

加入不加这个方法的话,scrapy就会根据默认的流程,对start_urls的链接进行请求下载执行,将返回值传给parse函数,parse负责解析处理请求内容。

但是加入了新的方法之后,可以添加请求头,也可以不在中间件设置代理,然后执行请求将请求结果迭代给parse函数。

1. import scrapy
2.
3. class TestSpider(scrapy.Spider):
4. name = 'GGSpider'
5. allowed_domains = ['GG.com']
6. start_urls = ['https://GG.com/']
7.
8. def start_requests(self):
9. for url in self.starturl:
10. googleHeaders = {
11. "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
12. "authority": "www.GG.com",
13. "user-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.66 Safari/537.36",
14. }
15. yield scrapy.Request(url,headers=googleHeaders)
16.
17. def parse(self, response):
18. print(response.text)

执行刚才编写好的spider:

scrapy crawl googleSpider

这个googleSpider就是刚才项目中的name,结果如下:

Python中Scrapy框架的代理使用_xml_04

很显然,谷歌当然没法正常访问的,之后就引出下一个部分,scrapy使用代理。

三、Scrapy代理设置

scrapy添加代理访问有两种方式,一个是在中间件设置代理,第二个就是在spider中请求前添加代理。比较适合账户认证的代理方式。

3.1 中间件添加代理

首先在中间件middlewares.py中,在最后加入如下代码:

1. class ProxyMiddleware(object):
2. def process_request(self,request,spider):
3. entry = 'http://{}:{}@{}:{}'.format("账户", "密码","host","port")
4. request.meta["proxy"] = entry

然后在setting.py中设置优先级:

1. DOWNLOADER_MIDDLEWARES = {
2. '你的项目名.middlewares.ProxyMiddleware': 100,
3. }

3.2 spider中设置代理

1. import scrapy
2.
3. class TestSpider(scrapy.Spider):
4. name = 'GGSpider'
5. allowed_domains = ['GG.com']
6. start_urls = ['https://GG.com/']
7.
8. def start_requests(self):
9. # 账密:
10. # 账密链接赋值给变量
11. entry = 'http://{}-zone-custom:{}@proxy.ipidea.io:2334'.format("帐号", "密码")
12. # api
13. # entry = 'http://{}'.format("api获取的ip代理")
14. # 传参meta迭代下一个方法
15. for url in self.starturl:
16. googleHeaders = {
17. "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
18. "authority": "www.GG.com",
19. "user-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.66 Safari/537.36",
20. }
21. yield scrapy.Request(url,headers=GGHeaders,meta={"proxy":entry})
22.
23. def parse(self, response):
24. print(response.text)

我这里使用的是ipidea的代理,好的代理能帮助你更好的获取数据,高质量低延迟,而且新用户可以白嫖哦!

地址:​ ​http://www.ipidea.net​

如果你要是访问GG的话,访问的过于频繁会出现429警告,建议不要高频访问,适当慢一点。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK