第一种方法是使用telnetlib
import telnetlib import requests from lxml import etree #解析此url页面的IP url = 'http://ip.geiwoxiao.com/' headers = { 'User-Agent':'User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;' } #使用requests请求指定页面,返回html页面 html = requests.get(url=url,headers=headers).text #使用xpath解析数据,拿到IP及端口 tree = etree.HTML(html) ip_text = tree.xpath('/html/body/div[5]/p[2]/text()')[0] ip_list = ip_text.split(' ') #循环ip_list,取出每个IP做检测是否可用 for i in ip_list: ip,port = i.split(':') print(ip,port) try: telnetlib.Telnet(ip,port,timeout=5) print('可用') except Exception as e: print(e,'不可用')
第二种方法
from lxml import etree import requests #解析此url页面的IP url = 'http://ip.geiwoxiao.com/' #使用requests请求指定页面,返回html页面 html = requests.get(url=url).text headers = { 'User-Agent':'User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;' } #使用xpath解析数据,拿到IP及端口 tree = etree.HTML(html) ip_text = tree.xpath('/html/body/div[5]/p[2]/text()')[0] ip_list = ip_text.split(' ') use_ip = [] #循环ip_list,取出每个IP做检测是否可用 for ip in ip_list: try: res = requests.get(url='http://icanhazip.com/',proxies={'http':ip},timeout=5).text print(res.strip()) if res.strip() in ip: print(ip,'可用') #将可用IP添加到列表中 use_ip.append(ip) else: print(ip,'不可用') except Exception as e: print(ip,'超时') #查看可用的use_ip print(use_ip)
第三种是访问百度
from lxml import etree import requests #解析此url页面的IP url = 'http://ip.geiwoxiao.com/' #使用requests请求指定页面,返回html页面 html = requests.get(url=url).text headers = { 'User-Agent':'User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;' } #使用xpath解析数据,拿到IP及端口 tree = etree.HTML(html) ip_text = tree.xpath('/html/body/div[5]/p[2]/text()')[0] ip_list = ip_text.split(' ') use_ip = [] #循环ip_list,取出每个IP做检测是否可用 for ip in ip_list: try: res = requests.get(url='https://www.baidu.com/',headers=headers,proxies={'https':ip},timeout=5).text print(len(res))#判断URL返回的数据长度是否大于5000 except Exception as e: print(ip,e)