Reputation: 171
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
Upvotes: 1
Views: 3217
Reputation: 3318
In order to reach your if statement, your code needs exception handling. An exception is being raised when you call urlopen
on line 7. See the first step of your traceback.
File "gd.py", line 7, in <module>
r = urllib.request.urlopen(url)
The exception happens here, which causes your code to exit, so further statements aren't evaluated. To get past this, you must 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")
Going beyond this, if you want something more like your original logic, I'd recommend using the requests library. I'd actually recommend using requests for all of your HTTP needs whenever possible, it's exceptionally good.
import requests
r = requests.get(url)
p = r.status_code
if r.status_code == 404:
print("hello")
else:
print("null")
Upvotes: 0
Reputation: 638
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")
Upvotes: 1