Python实现采集网站ip代理并检测是否可用

作者:松鼠爱吃饼干 时间:2021-01-10 09:10:53 

开发环境

Python 3.8

Pycharm

模块使用

requests >>> pip install requests

parsel >>> pip install parsel

代理ip结构

proxies_dict = {
   "http": "http://" + ip:端口,
   "https": "http://" + ip:端口,
}

代码实现步骤

1. 导入模块

# 导入数据请求模块
import requests  # 数据请求模块 第三方模块 pip install requests
# 导入 正则表达式模块
import re  # 内置模块
# 导入数据解析模块
import parsel  # 数据解析模块 第三方模块 pip install parsel  >>> 这个是scrapy框架核心组件

2. 发送请求

对于目标网址发送请求 https://www.kuaidaili.com/free/

url = f'https://www.kuaidaili.com/free/inha/{page}/'  # 确定请求url地址
# 用requests模块里面get 方法 对于url地址发送请求, 最后用response变量接收返回数据
response = requests.get(url)

3. 获取数据

获取服务器返回响应数据(网页源代码)

print(response.text)

4. 解析数据

提取我们想要的数据内容

解析数据方式方法:

  • 正则: 可以直接提取字符串数据内容

  • xpath: 根据标签节点 提取数据内容

  • css选择器: 根据标签属性提取数据内容

哪一种方面用那种, 那是喜欢用那种

正则表达式提取数据内容

正则提取数据 re.findall() 调用模块里面的方法

正则 遇事不决 .*? 可以匹配任意字符(除了换行符\n以外) re.S

ip_list = re.findall('<td data-title="IP">(.*?)</td>', response.text, re.S)
port_list = re.findall('<td data-title="PORT">(.*?)</td>', response.text, re.S)
print(ip_list)
print(port_list)

css选择器

css选择器提取数据 需要把获取下来html字符串数据(response.text) 进行转换

# #list > table > tbody > tr > td:nth-child(1)
# //*[@id="list"]/table/tbody/tr/td[1]
selector = parsel.Selector(response.text) # 把html 字符串数据转成 selector 对象
ip_list = selector.css('#list tbody tr td:nth-child(1)::text').getall()
port_list = selector.css('#list tbody tr td:nth-child(2)::text').getall()
print(ip_list)
print(port_list)

xpath 提取数据

selector = parsel.Selector(response.text) # 把html 字符串数据转成 selector 对象
ip_list = selector.xpath('//*[@id="list"]/table/tbody/tr/td[1]/text()').getall()
port_list = selector.xpath('//*[@id="list"]/table/tbody/tr/td[2]/text()').getall()

提取ip

for ip, port in zip(ip_list, port_list):
   # print(ip, port)
   proxy = ip + ':' + port
   proxies_dict = {
       "http": "http://" + proxy,
       "https": "http://" + proxy,
   }
   print(proxies_dict)

Python实现采集网站ip代理并检测是否可用

5. 检测ip质量

try:
   response = requests.get(url=url, proxies=proxies_dict, timeout=1)
   if response.status_code == 200:
       print('当前 * : ', proxies_dict,  '可以使用')
       lis_1.append(proxies_dict)
except:
   print('当前 * : ', proxies_dict,  '请求超时, 检测不合格')

print('获取的 * 数量: ', len(lis))
print('获取可用的IP代理数量: ', len(lis_1))
print('获取可用的IP代理: ', lis_1)

Python实现采集网站ip代理并检测是否可用

总共爬取了150个,最后测试出只有一个是能用的,所以还是付费的好

来源:https://www.cnblogs.com/qshhl/p/15834836.html

标签:Python,网站,ip代理
0
投稿

猜你喜欢

  • python如何在pygame中设置字体并显示中文详解

    2021-03-21 13:32:50
  • Python的字典和列表的使用中一些需要注意的地方

    2023-01-09 02:03:52
  • Python读取配置文件的实战操作

    2021-08-12 19:48:09
  • pytorch transform数据处理转c++问题

    2023-08-19 11:24:49
  • python安装本地whl的实例步骤

    2023-05-09 05:25:33
  • 一文带你搞懂Python中的文件操作

    2023-07-20 14:00:52
  • TensorFlow教程Softmax逻辑回归识别手写数字MNIST数据集

    2021-05-24 18:25:35
  • 微信小程序实现富文本图片宽度自适应的方法

    2023-10-17 12:44:25
  • python实现文件快照加密保护的方法

    2022-08-21 20:01:29
  • 网页设计详细教程之XML简便省力技巧五则

    2008-05-23 14:37:00
  • python程序快速缩进多行代码方法总结

    2022-12-06 03:30:38
  • 解决MSSQL下“不能在手动或分布事务方式下创建新的连接”的问题

    2008-07-15 12:48:00
  • 在Python文件中指定Python解释器的方法

    2023-06-24 13:22:50
  • Python中random模块生成随机数详解

    2023-06-13 16:11:52
  • 安装PyInstaller失败问题解决

    2022-03-18 04:21:41
  • Python try-except-else-finally的具体使用

    2022-10-18 14:31:45
  • python中正则表达式的使用方法

    2021-08-14 09:36:59
  • PHP中Too few arguments to function的问题及解决

    2023-06-04 21:15:55
  • 手残删除python之后的补救方法

    2021-04-13 12:50:04
  • 在ipython notebook中使用argparse方式

    2021-11-17 08:58:41
  • asp之家 网络编程 m.aspxhome.com