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

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

运行环境:
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))

创建

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值