python获取及时可用国外高匿名代理

#encoding=utf8

import urllib2
import cookielib 
import lxml.html
from __builtin__ import False
import threading
import time

ip_list = []#最后的可用列表

def prepare():
    cj = cookielib.MozillaCookieJar()  
    cookie_support = urllib2.HTTPCookieProcessor(cj)  
    opener = urllib2.build_opener(cookie_support)  
    opener.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.3.0')]
    urllib2.install_opener(opener) 

def page(j):
    prepare()
    try:
        url = 'http://www.xicidaili.com/wn/'+str(j)
        response = urllib2.urlopen(url)
        http = response.read().decode('utf-8')
        doc = lxml.html.fromstring(http)
        results = doc.xpath('//div/table[@id="ip_list"]/tr/td/text()')
        return results
    except:
        pass

def page_content(j):
    url = 'http://www.xicidaili.com/wn/'+str(j)
    results = page(j)
    for i in range(1,100):
        try:
            proxy = results[i*10]+":"+results[i*10+1]
            sContent = urllib2.urlopen(url,timeout=3).getcode()
            if(sContent == 200):
                print(proxy)
                ip_list.append(proxy)
            else:
                print("***")
        except:
            pass   



class everpage(threading.Thread):
    def __init__(self,page):
        threading.Thread.__init__(self)
        self.page = page
        self.thread_stop = False
        
    def run(self):
        while not self.thread_stop:
            page_content(self.page)

            self.stop()
            
    def stop(self):
        self.thread_stop = True


if __name__=='__main__':
    for i in range(1,100):
        try:
            everpage(i).start()
            time.sleep(5)
        except:
            pass


转载于:https://my.oschina.net/passer007/blog/638452

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值