V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
gogo123
V2EX  ›  Python

python 多线程每隔 2 分钟就重新进行代理验证,但时间一长程序就没有反应也不报错,一般什么原因会引起这种情况

  •  
  •   gogo123 · 2015-06-04 12:44:41 +08:00 · 3349 次点击
    这是一个创建于 3256 天前的主题,其中的信息可能已经有所发展或是发生改变。
    正在做一套爬虫,需要有效代理防止被封。

    方法是同时运行两个程序,一个爬虫程序、一个代理验证。代理验证是每隔2分钟就获取最新的代理列表依次验证ip有效性,有效的ip存入指定文件中,爬虫在读取这个文件的ip去抓数据。

    目前问题是代理验证也就运行3、4轮就没了反应,也不提示报错什么的,google下貌似是什么多线程队列还是IO阻塞啥的,没看明白。。
    第 1 条附言  ·  2015-06-04 19:46:02 +08:00
    class ProxyCheck(threading.Thread):
    def __init__(self,proxyList):
    threading.Thread.__init__(self)
    self.proxyList = proxyList
    self.timeout = 10
    self.testUrl = "http://www.baidu.com/"
    self.testStr = "030173"

    def checkProxy(self):
    cookies = urllib2.HTTPCookieProcessor()
    for proxy in self.proxyList:
    proxyHandler = urllib2.ProxyHandler({"http" : r'http://%s' % proxy})
    opener = urllib2.build_opener(cookies,proxyHandler)
    opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1')]
    t1 = time.time()

    try:
    req = opener.open(self.testUrl, timeout=self.timeout)
    result = req.read()
    timeused = time.time() - t1
    pos = result.find(self.testStr)

    title = re.search(r'<title>(.*?)</title>',urllib2.urlopen('http://www.baidu.com').read())

    if title:baidu_title = title.group(1)
    else:baidu_title = '无法打开网页'

    if (pos > -1) and (timeused < 5) and baidu_title == '百度一下,你就知道':checkedProxyList.append((proxy,timeused))
    else: continue

    except Exception,e:
    print e.message
    continue

    def sort(self):
    sorted(checkedProxyList,cmp=lambda x,y:cmp(x[1],y[1]))
    def run(self):
    self.checkProxy()
    self.sort()

    if __name__ == "__main__":
    getThreads = []
    checkThreads = []

    for i in range(35):
    t = ProxyCheck(rawProxyList[((len(rawProxyList)+34)/35) * i:((len(rawProxyList)+34)/35) * (i+1)])
    checkThreads.append(t)

    for i in range(len(checkThreads)):
    checkThreads[i].start()

    for i in range(len(checkThreads)):
    checkThreads[i].join()
    第 2 条附言  ·  2015-06-04 19:51:47 +08:00
    定时循环是通过while弄得

    while 1:
    os.system("python 代理验证.py")
    time.sleep(120)
    2 条回复    2015-06-04 14:35:05 +08:00
    aisk
        1
    aisk  
       2015-06-04 12:52:53 +08:00
    记得设置超时。
    est
        2
    est  
       2015-06-04 14:35:05 +08:00   ❤️ 1
    提出这样的问题,期待的答案是基本靠猜么。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1363 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 17:01 · PVG 01:01 · LAX 10:01 · JFK 13:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.