I am trying to read the url code using URLLIB. Here is my code:
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")
The url I am using will show error code 404 but I am not able to read it.
I also tried if(p == 404)
but I get the same issue.
I Can read other codes i.e. 200, 201 etc.
Can you please help me fix it?
traceback:
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
I'm not sure that's what you're asking.
import urllib.request
url = "https://www.facebook.com/fads0000fass"
try:
r = urllib.request.urlopen(url)
p = r.code
except urllib.error.HTTPError:
print("hello")