python中使用urllib2獲取http請求狀態(tài)碼的代碼例子
更新時間:2014年07月07日 10:51:10 投稿:junjie
這篇文章主要介紹了python中使用urllib2獲取http請求狀態(tài)碼的代碼例子,需要的朋友可以參考下
采集內(nèi)容常需要得到網(wǎng)頁返回的驗證碼做進一步處理
下面代碼是用python寫的用來獲取網(wǎng)頁http狀態(tài)碼的腳本
#!/usr/bin/python # -*- coding: utf-8 -*- #encoding=utf-8 #Filename:states_code.py import urllib2 url = 'http://www.dbjr.com.cn/' 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()
相關(guān)文章
利用Python批量循環(huán)讀取Excel的技巧分享
這篇文章主要為大家詳細介紹了何用Python批量循環(huán)讀取Excel,文中的示例代碼講解詳細,對我們的學習或工作有一定的幫助,感興趣的可以了解一下2023-07-07