Python獲取HTTP請(qǐng)求的狀態(tài)碼(200,404等)
問(wèn)題描述
Python獲取HTTP請(qǐng)求的狀態(tài)碼(200,404等),不訪問(wèn)整個(gè)頁(yè)面源碼,那樣太浪費(fèi)資源:
輸入:segmentfault.com 輸出:200輸入:segmentfault.com/nonexistant 輸出:404
問(wèn)題解答
回答1:參考文章:Python實(shí)用腳本清單
http不只有g(shù)et方法(請(qǐng)求頭部+正文),還有head方法,只請(qǐng)求頭部。
import httplibdef get_status_code(host, path='/'): ''' This function retreives the status code of a website by requestingHEAD data from the host. This means that it only requests the headers.If the host cannot be reached or something else goes wrong, it returnsNone instead. ''' try:conn = httplib.HTTPConnection(host)conn.request('HEAD', path)return conn.getresponse().status except StandardError:return Noneprint get_status_code('segmentfault.com') # prints 200print get_status_code('segmentfault.com', '/nonexistant') # prints 404回答2:
你用get請(qǐng)求就會(huì)請(qǐng)求整個(gè)頭部+正文, 可以試下head方法, 直接訪問(wèn)頭部!
import requestshtml = requests.head(’http://segmentfault.com’) # 用head方法去請(qǐng)求資源頭部print html.status_code # 狀態(tài)碼html = requests.head(’/nonexistant’) # 用head方法去請(qǐng)求資源頭部print html.status_code # 狀態(tài)碼# 輸出:200404
