zoukankan      html  css  js  c++  java
  • 20170918-00-(代理ip检验)

    代码集编号 20170918-00

    import random                                 #随机数模块
    import urllib.request                         #常用爬虫模块
    import time
    from bs4 import BeautifulSoup                #网页页面优化模块
    import re                                    #正则模块
    import json                                  #json数据分析模块
    
    url="http://www.whatismyip.com.tw/"
    real=input("请输入您的真实ip:")
    url1="http://www.baidu.com/"               #用代理ip访问百度用以验证
    
    #**********************************************************************************
    
    def text(textip):                                                                  #定义函数
        proxy_support = urllib.request.ProxyHandler({'http':textip})
        opener=urllib.request.build_opener(proxy_support)                               #接入代理ip
        opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36')]    #替换头部
        urllib.request.install_opener(opener)
        try:
            response = urllib.request.urlopen(url1, timeout=5)                          #访问百度,定义延迟时间
            x=0
        except:
            x=1
        return x
       
    #*******************************************************************************************************
    
    while 1==1:                                                                        #主程序
        textip=input("请输入要验证的ip和端口:")
        
        print("----------------------正在验证--------------------------")
        
        text(textip)
        x=text(textip)
        
        if x == 0:                                                                     #获取返回值
            response=urllib.request.urlopen(url)
            html = response.read().decode('utf-8')
    
            soup=BeautifulSoup(html,'html.parser')
            
            y=re.compile(r'<script id="ip-json" type="application/json">([sS]*?)</script>')
            a=y.findall(str(soup))
            
            dicip=eval(a[0])           #将字符串转化为字典
            print(dicip['ip-real'])                                                   #在url中找到自己的ip-real一项 并与自己输入的真时ip比较,从而判断是否为高匿ip
            if dicip['ip-real']==real:
              print("代理有效,但不属于匿名代理")
            else:
              print("代理有效,属于匿名代理")
        else:
            print("连接超时,该代理无效或者超时")
        
        print("-------------------------------------------------$$$$-------")
        
    #**************************************************************************************************************
       # 本代码 @  1,使用代理ip    2,隐藏(替换)头部   3,爬虫基本代码   4,将字符串转化为字典 函数 eval()
  • 相关阅读:
    C
    A
    L
    G
    关于html()、val()、text()
    EL表达式
    JSON书写格式示例
    Servlet获取项目名的方法
    修改完Servlet后不用重启项目的设置方法
    浮动
  • 原文地址:https://www.cnblogs.com/jjj-fly/p/7545101.html
Copyright © 2011-2022 走看看