我有一个脚本,在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
谢谢大家!你知道吗
一个while循环是清晰的,结构良好,符合相当多的语言标准。你知道吗
相关问题 更多 >
编程相关推荐