Python USES urllib2 to get an example of the HTTP request status code

  • 2020-04-02 13:47:47
  • OfStack

The collected content often needs to get the verification code returned from the web page for further processing

The following code is a script written in python to get the HTTP status code for the web page


#!/usr/bin/python
# -*- coding: utf-8 -*-
#encoding=utf-8
#Filename:states_code.py
 
import urllib2
 
url = '//www.jb51.net/'
response = None
try:
  response = urllib2.urlopen(url,timeout=5)
except urllib2.URLError as e:
  if hasattr(e, 'code'):
    print 'Error code:',e.code
  elif hasattr(e, 'reason'):
    print 'Reason:',e.reason
finally:
  if response:
    response.close()


Related articles: