urllib2 catches 404 error while URL exists

2019-08-05 16:59发布

I faced with strange bug: urllib2 catches 404 error, while openning a valid url. I tryed it in browser, the url can be opened. Also I pass user-agent.

import urllib.request as urllib2
uri = 'https://i.ytimg.com/vi/8Sii8G5CNvY/hqdefault.jpg?custom=true&w=196&h=110&stc=true&jpg444=true&jpgq=90&sp=68&sigh=OIIIAPOKNtx1OiZbAqdORlzl92g'
try:
  req = urllib2.Request(uri, headers={ 'User-Agent': 'Mozilla/5.0' })
  file = urllib2.urlopen(req)
except urllib2.HTTPError as err:
  if err.code == 404:
    return "Not Found"

Why I get this error? Thank you for answers.

1条回答
smile是对你的礼貌
2楼-- · 2019-08-05 17:32

If you want to get the body anyway, simply read the error response with an err.read():

import urllib2
uri = 'https://i.ytimg.com/vi/8Sii8G5CNvY/hqdefault.jpg?custom=true&w=196&h=110&stc=true&jpg444=true&jpgq=90&sp=68&sigh=OIIIAPOKNtx1OiZbAqdORlzl92g'
try:
  req = urllib2.Request(uri, headers={ 'User-Agent': 'Mozilla/5.0' })
  file = urllib2.urlopen(req)
except urllib2.HTTPError as err:
  if err.code == 404:
    print "Not Found"
    print err.read()
查看更多
登录 后发表回答