1

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
5
  • Does this answer your question, stackoverflow.com/questions/29658375/… Commented May 23, 2020 at 16:42
  • Pick a python version. Urllib varies from 2 to 3. Commented May 23, 2020 at 17:10
  • @Sushanth No sorry. it doesnt Commented May 23, 2020 at 17:11
  • @SamMorgan I am not sure but i think its urllib2. But the basic features are same I guess. Also, I can make changes according to the answer if this is the issue Commented May 23, 2020 at 17:13
  • @bitch_ctrl What I meant was: You have multiple Python tags here. urllib varies from Python 2 to Python 3, and for anyone to give you a useful answer, they'll need to know what environment you're running in. Side note: I'd highly recommend using the requests library for HTTP, it's sort of become the unofficial standard. Commented May 23, 2020 at 18:34

2 Answers 2

1

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")
Sign up to request clarification or add additional context in comments.

Comments

0

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.