写这个的方式不同

2024-06-02 06:22:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,在python中使用requests包时有时会捕获ChunckedEncodingError。你知道吗

我已通过执行以下操作成功解决了此问题:

try:
    _, status, html_data, _ = lib.core.common.get_page(url, agent=agent, proxy=proxy)
except requests.exceptions.ChunkedEncodingError:
    lib.core.settings.logger.warning(lib.core.settings.set_color(
        "encoding seems to be messed up, trying the request again...", level=30
    ))
    try:
        _, status, html_data, _ = lib.core.common.get_page(url, agent=agent, proxy=proxy)
    except requests.exceptions.ChunkedEncodingError:
        lib.core.settings.logger.error(lib.core.settings.set_color(
            "encoding is unable to be fixed from a retry, skipping...", level=40
        ))
        return False, None
except requests.exceptions.InvalidURL:
    url = "http://{}".format(url)
    _, status, html_data, _ = lib.core.common.get_page(url, agent=agent, proxy=proxy)

现在这个很管用,但是我不太喜欢try块中的整个except,有没有其他方法可以让我写的更具pythonic和可读性?你知道吗


根据提供的答案,我得出了以下结论:

retry_flags = 3
auto_assign = "http://{}"
url_verification = re.compile(r"http(s)?", re.I)

if url_verification.search(url) is None:
    lib.core.settings.logger.warning(lib.core.settings.set_color(
        "protocol missing from URL, automatically assigning protocol...", level=30
    ))
    url = auto_assign.format(url)

while retry_flags > 0:
    try:
        _, status, html_data, _ = lib.core.common.get_page(url, agent=agent, proxy=proxy)
    except requests.exceptions.ChunkedEncodingError:
        lib.core.settings.logger.warning(lib.core.settings.set_color(
            "encoding seems to be messed up, retrying request...", level=30
        ))
        retry_flags -= 1
return False, None

谢谢大家!你知道吗


Tags: coreurldatagetsettingslibhtmlstatus
1条回答
网友
1楼 · 发布于 2024-06-02 06:22:10

一个while循环是清晰的,结构良好,符合相当多的语言标准。你知道吗

def get_url_wrapper(url, agent, proxy):
    flag = 3

    while flag > 0:
        try:
           _, status, html_data, _ = lib.core.common.get_page(url, agent=agent, proxy=proxy)
           return status, html_data
        except requests.exceptions.InvalidURL:
            # This is a case of invalid structure. Don't modify flag
            url = "http://{}".format(url)
        except requests.exceptions.ChunkedEncodingError:
            lib.core.settings.logger.error(lib.core.settings.set_color(
                "encoding is unable to be fixed from a retry, skipping...", level=40
            ))
            # this is an actual error, decrement flag.
            flag -= 1

    return False, None

相关问题 更多 >