Python 抓取免费的代理IP地址和端口号,并检测稳定性

本文介绍了一种使用Python从快代理网站抓取免费IP地址和端口号的方法,通过创建不同模块分别负责抓取、检测IP,并将稳定IP保存。代码中运用了requests、BeautifulSoup和lxml等库,抓取后的IP经过测试,可用的被写入到新的文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

运行环境:
MacBook Pro 10.14 python3.6.2 pycharm 2018.3.2
运用到的模块:
requests、BeaetifulSoup、lxml、time、random、os
创建主函数:Grasp_ip.py 负责实现抓取IP,post的业务逻辑
#encoding=utf-8
import requests
from header import headers
from bs4 import BeautifulSoup
from urls import urls
import time
import random

n=0
for url in urls():
request=requests.get(url,headers=headers())
request.encoding=‘gbk’
html=BeautifulSoup(request.text,features=‘lxml’)
tr=html.find_all(‘tr’)
f=open(‘kuaidaili_ip.txt’, mode=‘a+’)
for td in tr[1:len(tr)]:
ip=td.find_all(‘td’)[0].text
post=td.find_all(‘td’)[1].text
ip_post=ip+’\t’+post+’\n’
f.write(ip_post)
n=n+1
print(‘成功抓取第’+str(n)+‘页!’)
time.sleep(random.randint(2,10))

创建 headers.py 存放请求头信息
#encoding=utf-8
def headers():
headers={}
User_Agent = ‘Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值