-1

I want to handle http.client.BadStatusLine: '' exception. I am on python3. My code is

import http.client

try:
    req = urllib.request.Request(url, headers={'User-Agent': 'Chrome/51'})
    html = urllib.request.urlopen(req).read()
    soup = BeautifulSoup(html,"html.parser")
    return soup
except http.client.HTTPException as eror:
    print("Boom")

but httpException is not in python3? Figure

I read stackOverFloeQuestion and this but unable to catch it. Any help?

Community
  • 1
  • 1
Ali
  • 85
  • 1
  • 1
  • 9
  • It is in the docs: https://docs.python.org/3/library/http.client.html#http.client.HTTPException What is the actual error that you recieve? – DeepSpace Aug 03 '16 at 14:03
  • @DeepSpace. I want to catch this exception. That is it. Red sign in the above picture is showing that `HTTPException` is not defined. – Ali Aug 03 '16 at 14:43
  • Copy-pasting your code doesn't reproduce that issue. – DeepSpace Aug 03 '16 at 14:45
  • Copy-Pasting? Sorry . You did not understand the question. @Hans Then resolve it. Thanks. – Ali Aug 03 '16 at 14:50

1 Answers1

0

You need to make up your mind :-). You can either use http.client or urllib.request, but you should not use one and then try to catch errors from the other. If you want to stick with urllib.request, the class to catch is urllib.error.HTTPError.

Perhaps it may be more opportune in your case to use Requests, which is a higher level http library.

Hans Then
  • 10,935
  • 3
  • 32
  • 51
  • I want to be specific to `BadStatusLine` Exception. Not whole `HTTPError` – Ali Aug 03 '16 at 14:44
  • While this is logically correct, it still doesn't explain why would `http.client.HTTPException` be not defined. Especially when copy-pasting OPs code doesn't reproduce the issue. – DeepSpace Aug 03 '16 at 14:45
  • I think BadStatusLine is defined in httplib. So perhaps try `import httplib.BadStatusLine` and catch that error. – Hans Then Aug 03 '16 at 15:49