写这个的方式不同

时间:2017-12-04 19:38:12

标签: python python-2.7 exception-handling python-requests

我有一个脚本,有时会在python中使用ChunckedEncodingError包时捕获requests

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

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

谢谢大家!

1 个答案:

答案 0 :(得分:2)

while循环很清晰,并且由很多语言标准构建得很好。

Traceback (most recent call last):
File "C:\Users\Bradley Jo\AppData\Local\Programs\Python\Python36\lib\site-
packages\selenium\webdriver\common\service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
 File "C:\Users\Bradley 
Jo\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 709, in 
__init__
restore_signals, start_new_session)
File "C:\Users\Bradley 
Jo\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 997, in 
_execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
  File "C:\Users\Bradley Jo\Desktop\Project\app.py", line 3, in 
 <module>
 browser = webdriver.Chrome()
  File "C:\Users\Bradley Jo\AppData\Local\Programs\Python\Python36\lib\site-
  packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
 self.service.start()
 File "C:\Users\Bradley Jo\AppData\Local\Programs\Python\Python36\lib\site-    
 packages\selenium\webdriver\common\service.py", line 81, in start
 os.path.basename(self.path), self.start_error_message)
 selenium.common.exceptions.WebDriverException: Message: 'chromedriver'     
 executable needs to be in PATH. Please see     
 https://sites.google.com/a/chromium.org/chromedriver/home

[Finished in 1.0s]