Python uses urllib2 to obtain the http Request status code. For example, pythonurllib2
The verification code returned by the webpage is often needed for further processing.
The following code is a script written in python to obtain the http status code of a webpage.
#! /Usr/bin/python #-*-coding: UTF-8-*-# encoding = UTF-8 # Filename: states_code.py import urllib2 url = 'HTTP: // www.bkjia.com/'response = Nonetry: response = urllib2.urlopen (url, timeout = 5) response t urllib2.URLError as e: if hasattr (e, 'code'): print 'error code: ', e. code elif hasattr (e, 'reason '): print 'reason:', e. reasonfinally: if response: response. close ()
Http problems in python urllib2
Print 2 is returned only when the try code has no exception. If you say print 2 is never executed, there is a problem in the try code block.
In python, how does one obtain the webpage source code with a non-200 return code?
Non-200 may not have source code. You can refer to RFC2616, for example, 302 and 301. Then you can request the Location url in the header, and you can give up 5xx ......