简体   繁体   English

python缩短嵌入式try-except块

[英]python Shorten the embedded try-except block

I've been using try-except inside another try-except , when I try to open a url which may or may not lack an 'http://' header. 我一直在使用try-except在另一个try-except ,当我尝试打开一个可能有也可能没有'http://'标题的url时。

But the code looks messy. 但是代码看起来很混乱。 I'm wondering if python has some nicer ways to deal with such needs. 我想知道python是否有更好的方法来处理这些需求。 Actually I've read the 'with' keyword.. Which somehow, I feel, will make the code harder to read... 实际上,我已经读过'with'关键字。.我觉得这会使代码更难阅读...

I've got links to open, but the links may have something missing. 我有链接打开,但链接可能有一些缺失。 For example the url is like. 例如,URL就像。 either xxx.com or http : / / xxx . xxx.com或http:// xxx。 com. COM。 And I have to test http:// and url, http:// plus www. 而且我必须测试http://和url,http://加上www。 plus url, url to find the right one. 加上网址,网址找到合适的网址。

And if none works, I must record the reason why, and go on with the next link: 如果没有,我必须记录原因,然后继续下一个链接:

    for link in links:
        url = link
        if not url.startswith('http'):
            try:
                url1 = r'http://'+link
                res = requests.get(url)
            except Exception as e:
                try:
                    url2 = r'http://www.'+link
                    res = requests.get(url)
                except Exception as err:
                    self.print_error(*[url1, modify(e.message)])
                    self.print_error(*[url2, modify(err.message)])
                    self.error_log(*[url1, modify(e.message)])
                    self.error_log(*[url2, modify(err.message)])

        else:
            try:
                res = request.get(url)
            except Exception as e:
                self.print_error(*[url, modify(e.message)])
                self.error_log(*[url, modify(e.message)])

First assemble a list of potential URLs in order of priority: 首先,按照优先级顺序组装潜在网址的列表:

potential_urls = [link, 'http://' + link, 'http://www.' + link]

Also keep a list of errors you've encountered: 同时保留您遇到的错误的列表:

errors_encountered = []

Then go through the list, break ing if it works. 然后遍历列表,如果可行则break

res = None
for url in potential_urls:
    try:
        res = requests.get(url)
    except Exception as err:
        errors_encountered.append(err)
    else:
        break

If none of the attempts work, we'll leave res set to None , which we can detect and log as desired: 如果没有尝试有效,我们将res设置为None ,我们可以根据需要检测并记录:

if res is None:
    for url, err in zip(potential_urls, errors_encountered):
        self.print_error(url, modify(err.message))
    for url, err in zip(potential_urls, errors_encountered):
        self.error_log(url, modify(err.message))

Otherwise, it succeeded, and you can use res as usual. 否则,它成功了,你可以照常使用res

Here is one other possibility, which simply reduces the nesting by continuing through the loop on success. 这是另一种可能性,它通过在成功时继续循环来简化嵌套。

for link in links:
    url = link
    if not url.startswith('http'):
        try:
            url1 = r'http://'+link
            res = requests.get(url)
            continue # we succeeded
        except Exception as e:
            pass

        try:
            url2 = r'http://www.'+link
            res = requests.get(url)
        except Exception as err:
            self.print_error(*[url1, modify(e.message)])
            self.print_error(*[url2, modify(err.message)])
            self.error_log(*[url1, modify(e.message)])
            self.error_log(*[url2, modify(err.message)])

Similar to @icktoofay's answer, but working from the perspective that you probably only need to work if the link succeeded or not. 与@icktoofay的答案类似,但是从这样的角度来看,如果链接成功与否,您可能只需要工作即可。

Define a function that does possible attempts and either returns a requests object, or after attempting all possibilities, raises an exception. 定义执行可能尝试的函数,并返回请求对象,或在尝试所有可能性之后,引发异常。

def try_get(link):
    for url in (link, 'http://' + link, 'http://www.' + link):
        try:
            return requests.get(url)
        except Exception:
            pass
    raise ValueError('no url was suitable', link)

Then loop over your urls, and either use res or do something suitable with the exception. 然后循环访问您的网址,并使用res或执行例外操作。

for url in list_of_urls:
    try:
        res = try_get(url)
    except ValueError as e:
        print e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM