loading...
爬虫日记-采集 快代理 免费 代理ip 并 清洗 ip
Published in:2021-12-08 | category: 磨刀不误砍柴工
Words: 1.1k | Reading time: 5min | reading:

1
2
3
4
5
6
环境
window10
python3
使用到的库
requests 网络请求
lxml 网页源码解析

源码地址

[TOC]

摘要

写爬虫被封ip是很正常的,所以代理ip也是爬虫生活必不可少的一部分,今天写一个网络上公开的免费的代理ip。

首先理一下逻辑,我要先写一个爬虫,去把网站上的公开的代理ip给采集过来,然后保险起见,对数据进行一定的清洗,获得可以使用的ip。

ok,开始写爬虫。

1 采集代理ip

1.1 观察目标网页

快代理免费ip展示的数据如下:

而我们程序进行网络访问所需要的部分为ipport类型,举个例子:

1
2
3
url = "https://www.baidu.com"
proxies = {'http': "http://111.231.86.149:7890"}
requests.get(href, proxies=proxies)

所以我们要采集的就是每个代理ip的ipport类型

1.2 采集

爬虫很简单,直接上代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 目标网址
url = "https://www.kuaidaili.com/free/"
payload = {}
# 构造请求头
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
'Accept': 'application/json, text/javascript, */*; q=0.01',
}
# 获取网页源码
response = requests.request("GET", url, headers=headers, data=payload)
# 存放结果的列表
res = []
_ = etree.HTML(response.text)
# 格式化代理的中间变量
type_dct = {
"HTTP": "http://",
"HTTPS": "https://"
}
data_list = _.xpath("//tbody/tr")
for data in data_list:
# 获取ip
ip = data.xpath("./td[1]/text()")[0]
port = data.xpath("./td[2]/text()")[0]
type = data.xpath("./td[4]/text()")[0]
# 存到结果中
res.append(type_dct[type] + ip + ':' + port)
print(res)

2 清洗代理

2.1 目的及原理

毕竟是网络上公开的数据,要是百分百好用的话,那为什么还要有付费的代理呢。

直接获取到的代理一般会有一定的时效,真假的缺陷,直接放进项目中去用的话,效率可能堪忧,所以在使用之前,最好对获取到的代理进行一定的清洗,获取暂时有效的代理。

清洗的方法也很简单,就是检测这个代理是否有效。

我们可以构造一个请求,用这个代理去进行访问,如果能访问的到,拿得到数据,那这个代理暂时是有效的,可以放到项目中去使用,如果直接404或者其他的一些问题出现,那这个代理可能已经坏了,或者质量不行,就直接放弃。

2.2 清洗ip的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
href = 'https://www.baidu.com'
if 'https' in proxy:
proxies = {'https': proxy}
else:
proxies = {'http': proxy}
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4396.0 Safari/537.36'
}
try:
r = requests.get(href, proxies=proxies, timeout=5, headers=headers)
if r.status_code == 200:
print "代理有效"
except:
print "代理失效"

3 完善代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import requests
from lxml import etree


# 获取快代理首页的代理
def get_proxy_list():
url = "https://www.kuaidaili.com/free/"
payload = {}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
'Accept': 'application/json, text/javascript, */*; q=0.01',
}
response = requests.request("GET", url, headers=headers, data=payload)
res = []
_ = etree.HTML(response.text)
type_dct = {
"HTTP": "http://",
"HTTPS": "https://"
}
data_list = _.xpath("//tbody/tr")
for data in data_list:
ip = data.xpath("./td[1]/text()")[0]
port = data.xpath("./td[2]/text()")[0]
type = data.xpath("./td[4]/text()")[0]
res.append(type_dct[type] + ip + ':' + port)
return res


# 测试代理
def check(proxy):
href = 'https://www.baidu.com'
if 'https' in proxy:
proxies = {'https': proxy}
else:
proxies = {'http': proxy}
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4396.0 Safari/537.36'
}
try:
r = requests.get(href, proxies=proxies, timeout=5, headers=headers)
if r.status_code == 200:
return True
except:
return False


if __name__ == '__main__':
proxy_list = get_proxy_list()
print(proxy_list)
for p in proxy_list:
print(p, check(p))

写在最后

代码并不完善,直接应用到像样的项目上还是不行的,也没有做一些奇怪操作、情况的处理,只是提供一个思路和一个demo。

实力有限,才疏学浅,如有错误,欢迎指正。

Prev:
谷歌 浏览器调试 开发者工具使用记录
Next:
爬虫日记-豆瓣新书速递
catalog
catalog