本文介绍了如何在一段时间内摆脱尝试/除外?[Python]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这个简单的代码,但该死的中断不起作用......出了什么问题?

I'm trying this simple code, but the damn break doesn't work... what is wrong?

while True:
    for proxy in proxylist:
        try:
            h = urllib.urlopen(website, proxies = {'http': proxy}).readlines()
            print 'worked %s' % proxy
            break
        except:
            print 'error %s' % proxy
print 'done'

当连接工作时它应该离开,如果没有则返回并尝试另一个代理

It's supposed to leave the while when the connection work, and go back and try another proxy if it didn't

好的,这就是我正在做的

ok, here is what I'm doing

我正在尝试检查一个网站,如果它发生了变化,它必须中断一段时间才能继续执行脚本的其余部分,但是当代理未连接时,我从变量中收到错误消息,如它是空的,所以我想要的是作为循环来尝试代理,如果它工作,继续脚本,脚本结束,返回并尝试下一个代理,如果下一个不起作用,会回到开头尝试第三个代理,依此类推..

I'm trying to check a website and if it changed, it has to break out of the while to continue to the rest of the script, but when the proxy doesn't connect, I get error from the variable, as it's null, so what I want is this for work as loop to try a proxy, and if it work, continue the script, and the end of the script, go back and try the next proxy, and if the next doesn't work, it will be back to the beginning to try the third proxy, and so on....

我正在尝试这样的事情

while True:
    for proxy in proxylist:
        try:
            h = urllib.urlopen(website, proxies = {'http': proxy})
        except:
            print 'error'
        check_content = h.readlines()
        h.close()
        if check_before != '' and check_before != check_content:
            break
        check_before = check_content
        print 'everything the same'
print 'changed'

推荐答案

你只是跳出了 for 循环 -- 而不是 while 循环:

You just break out of for loop -- not while loop:

running = True
while running:
    for proxy in proxylist:
        try:
            h = urllib.urlopen(website, proxies = {'http': proxy}).readlines()
            print 'worked %s' % proxy
            running = False
        except:
            print 'error %s' % proxy
print 'done'

这篇关于如何在一段时间内摆脱尝试/除外?[Python]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:16