blackball9

第一种方法是使用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(\'\r\n\')

#循环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(\'\r\n\')
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(\'\r\n\')
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)

 

 

posted on 2019-12-03 21:17  blackpearl9  阅读(3196)  评论(0编辑  收藏  举报

分类:

技术点:

相关文章:

  • 2021-12-06
  • 2021-05-01
  • 2021-11-29
  • 2021-11-05
  • 2021-08-06
  • 2021-11-16
  • 2021-11-15
  • 2021-12-14
猜你喜欢
  • 2021-12-06
  • 2021-12-06
  • 2021-12-15
  • 2021-10-03
  • 2021-12-19
  • 2021-12-06
  • 2021-12-06
相关资源
相似解决方案