我正在尝试使用URLLIB读取url代码。这是我的代码:
import urllib
url = "https://www.facebook.com/fads0000fass"
r = urllib.request.urlopen(url)
p = r.code
if(p == "HTTP Error 404: Not Found" ):
print("hello")
else:
print("null")
我使用的网址将显示错误代码404,但我无法读取它。
我也尝试过if(p == 404)
,但遇到了同样的问题。
我可以读取其他代码,例如200、201等
您能帮我解决吗?
回溯:
Traceback (most recent call last):
File "gd.py", line 7, in <module>
r = urllib.request.urlopen(url)
File "/usr/lib64/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib64/python3.7/urllib/request.py", line 531, in open
response = meth(req, response)
File "/usr/lib64/python3.7/urllib/request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib64/python3.7/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/usr/lib64/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/usr/lib64/python3.7/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
答案 0 :(得分:1)
我不确定这就是您要的内容。
import urllib.request
url = "https://www.facebook.com/fads0000fass"
try:
r = urllib.request.urlopen(url)
p = r.code
except urllib.error.HTTPError:
print("hello")
答案 1 :(得分:0)
为了到达您的if语句,您的代码需要异常处理。当您在第7行调用urlopen
时,会引发异常。请参见追溯的第一步。
File "gd.py", line 7, in <module>
r = urllib.request.urlopen(url)
此处发生异常,这将导致您的代码退出,因此不会评估其他语句。要克服这个问题,您必须handle the exception。
import urllib.request
url = "https://www.facebook.com/fads0000fass"
try:
r = urllib.request.urlopen(url)
except urllib.error.HTTPError as e:
# More useful
# print(f"{e.code}: {e.reason}\n\n{e.headers}")
if e.code in [404]:
print("hello")
else:
print("null")
除此之外,如果您想要更多类似于原始逻辑的内容,我建议使用requests库。实际上,我建议尽可能使用请求来满足您所有的HTTP需求,这非常好。
import requests
r = requests.get(url)
p = r.status_code
if r.status_code == 404:
print("hello")
else:
print("null")