• Python3 爬取验证代理


    前言

    看了一些文章,于是自己也尝试写一下

    实验

    直接放代码了,之前写好文章了,网掉了,文章直接没了,所以不重写了,直接写代码吧。有空改成多线程去验证IP,单线程太慢。

    验证IP途径:

    http://icanhazip.com/

    https://ifconfig.me/ip

    import requests
    import re
    def PaQu(num):
        url="http://www.66ip.cn/nmtq.php?getnum="+str(num)
        headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 12_10) AppleWebKit/600.1.25 (KHTML, like Gecko) Version/12.0 Safari/1200.1.25'}
        r=requests.get(url,headers=headers).text
        #quan_porxy=re.findall("((?:[0-9]{1,3}.){3}[0-9]{1,3}:d{1,5})", r)
        ip_proxy=re.findall("((?:[0-9]{1,3}.){3}[0-9]{1,3}):d{1,5}", r)#正则匹配IP
        port_proxy=re.findall("(?:[0-9]{1,3}.){3}[0-9]{1,3}:(d{1,5})", r)#正则匹配Port
        text_proxy=dict(zip(ip_proxy,port_proxy))
        return text_proxy
    def YanZeng(text_proxy):
        proxies={}
        headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 12_10) AppleWebKit/600.1.25 (KHTML, like Gecko) Version/12.0 Safari/1200.1.25'}
        for ip,port in text_proxy.items():
            proxies['http']='%s:%s'%(ip,port)
            print(f'Ip-Text:{ip}:{port}')
            try:
                Test=requests.get("http://icanhazip.com/",headers=headers,proxies=proxies,timeout=4).text
                if ip in Test:
                    print(f'{ip}:{port} online')
                    with open("ip.txt",'a') as file:
                        file.write(f'{ip}:{port}'+'
    ')
            except:
                pass
        print('Task Done')
    
    
    
    
    YanZeng(PaQu(50))

    发现其实爬下来并且验证了下ip代理,即便是可以用的,很不稳定,刚爬下来,用代理连直接就不行了。所以用的时候可以再验证一遍。毕竟是免费的代理,所以很不稳定也是正常。

  • 相关阅读:
    变长参数表
    以二进制方式输出数字在内存中的存储形式
    asp.net mvc controller 获取数据
    asp.net mvc 3.0 新特性之 Model
    一个实体对象不能由多个 IEntityChangeTracker 实例引用
    iis6.0运行asp请求资源正在使用中
    view的使用
    asp.net mvc RenderAction 和RenderPartial用法
    今天
    当下的力量
  • 原文地址:https://www.cnblogs.com/BOHB-yunying/p/12802075.html
Copyright © 2020-2023  润新知