zoukankan      html  css  js  c++  java
  • 获取免费代理IP库

    #!/usr/local/bin/python3
    # coding:utf-8
    
    # ====================================================
    # Author: chang - EMail:changbo@hmg100.com
    # Last modified: 2017-4-22
    # Filename: iplibrary.py
    # Description: get ip library files,base urlib, re
    # blog:http://www.cnblogs.com/changbo
    # ====================================================
    
    import urllib.request
    import re
    
    
    def filter_tags(htmlstr):
        re_cdata = re.compile('//<![CDATA[[^>]*//]]>', re.I)  # 匹配CDATA
        re_script = re.compile('<s*script[^>]*>[^<]*<s*/s*scripts*>', re.I)  # Script
        re_style = re.compile('<s*style[^>]*>[^<]*<s*/s*styles*>', re.I)  # style
        # re_br = re.compile('<brs*?/?>')  # 处理换行
        re_h = re.compile('</?w+[^>]*>')  # HTML标签
        re_comment = re.compile('<!--[^>]*-->')  # HTML注释
        s = re_cdata.sub('', htmlstr)  # 去掉CDATA
        s = re_script.sub('', s)  # 去掉SCRIPT
        s = re_style.sub('', s)  # 去掉style
        # s = re_br.sub('
    ', s)  # 将br转换为换行
        # s = re_h.sub('', s)  # 去掉HTML 标签
        s = re_comment.sub('', s)
        blank_line = re.compile('
    +')
        s = blank_line.sub('
    ', s)
        return s
    
    
    def getiplist(ipnumber):
        # url = 'http://ip.taobao.com/service/getIpInfo.php?ip=%s' % ip
        url = 'http://www.89ip.cn/api/?&tqsl=%d&sxa=&sxb=&tta=&ports=&ktip=&cf=1' % ipnumber
        f = urllib.request.Request(url)
        f.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0')
        response = ((urllib.request.urlopen(f)).read()).decode('gbk')
        ipinfo = filter_tags(response)
        # print(response)
        iplist = (((ipinfo.split('<br/>')[1])[:-46]).strip()).split('<BR>')
        for i in iplist:
            iptmp = i.split(':')
            ip = iptmp[0]
            port = iptmp[1]
            print(ip + ' ---- ' + port)
    getiplist(30)

    END!

  • 相关阅读:
    C#汉字转拼音(支持多音字)
    第15组构建之法团队心得
    初读《构建之法》之所感
    《构建之法》第二章自习之所感
    编程之美1的数目
    c# 将json转换为DataTable
    easyui datagrid deleteRow(删除行)的BUG
    sql 数据库 清空后让表的id从1开始等数据库操作
    sqlite字段类型说明
    webrequest、httpwebrequest、webclient、HttpClient 四个类的区别
  • 原文地址:https://www.cnblogs.com/changbo/p/6747624.html
Copyright © 2011-2022 走看看