不同的写法
Different way to write this
我有一个脚本,在 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)
现在一切正常了,但我不太喜欢 except
块中的整个 try
,有没有其他方法可以让我写得更 python ic 和可读性?
根据提供的答案,我得出以下结论:
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
我有一个脚本,在 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)
现在一切正常了,但我不太喜欢 except
块中的整个 try
,有没有其他方法可以让我写得更 python ic 和可读性?
根据提供的答案,我得出以下结论:
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