Python 写这篇文章的不同方式
我有一个脚本,在使用python中的Python 写这篇文章的不同方式,python,python-2.7,exception-handling,python-requests,Python,Python 2.7,Exception Handling,Python Requests,我有一个脚本,在使用python中的请求包时,它有时会捕获chuncedencodingerror 我已通过执行以下操作成功解决了此问题: try: _, status, html_data, _ = lib.core.common.get_page(url, agent=agent, proxy=proxy) except requests.exceptions.ChunkedEncodingError: lib.core.settings.logger.warning(lib
请求
包时,它有时会捕获chuncedencodingerror
我已通过执行以下操作成功解决了此问题:
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
,除了
块之外,还有其他方法可以让我写的更通俗易懂吗
根据提供的答案,我能够得出以下结论:
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
谢谢大家 一个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
根据许多语言标准,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
finally
调用在这里能派上用场吗?在循环中尝试除外并在成功时中断吗?在循环中尝试除外并在成功时中断吗?因此,我没有捕获InvalidURL
异常,而是添加了一个简单的正则表达式(re.compile(r“http(s)”,re.I.search(url)
)并且在连接之前添加了协议(如果没有),从那里我使用了你的while循环思想,它似乎工作得很好,谢谢。因此,我没有捕获InvalidURL
异常,而是添加了一个简单的正则表达式(re.compile(r“http(s)”,re.I)。搜索(url)
),并在连接之前添加了协议(如果没有),从那里我使用了你的while循环的想法,它似乎工作得很好,谢谢。