Python爬蟲headers處理及網絡超時問題解決方案
1、請求headers處理
我們有時請求服務器時,無論get或post請求,會出現(xiàn)403錯誤,這是因為服務器拒絕了你的訪問,這時我們可以通過模擬瀏覽器的頭部信息進行訪問,這樣就可以解決反爬設置的問題。
import requests # 創(chuàng)建需要爬取網頁的地址 url = 'https://www.baidu.com/' # 創(chuàng)建頭部信息 headers = {'User-Agent':'OW64; rv:59.0) Gecko/20100101 Firefox/59.0'} # 發(fā)送網絡請求 response = requests.get(url, headers=headers) # 以字節(jié)流形式打印網頁源碼 print(response.content)
結果:
b'<!DOCTYPE html><!--STATUS OK-->\n\n\n \n \n <html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta content="always" name="referrer"><meta name="theme-color" content="#2932e1"><meta name="description" content="\xe5\x85\xa8\xe7\x90\x83\xe6\x9c\x80\xe5\xa4\xa7\xe7\x9a\x84\xe4\xb8\xad\xe6\x96\x87\xe6\x90\x9c\xe7\xb4\xa2\xe5\xbc\x95\xe6\x93\x8e\xe3\x80\x81\xe8\x87\xb4\xe5\x8a\x9b\xe4\xba\x8e\xe8\xae\xa9\xe7\xbd\x91\xe6\xb0\x91\xe6\x9b\xb4\xe4\xbe\xbf\xe6\x8d\xb7\xe5\x9c\xb0\xe8\x8e\xb7\xe5\x8f\x96\xe4\xbf\xa1\xe6\x81\xaf\xef\xbc\x8c\xe6\x89\xbe\xe5\x88\xb0\xe6\x89\x80\xe6\xb1\x82\xe3\x80\x82\xe7\x99\xbe\xe5\xba\xa6\xe8\xb6\x85\xe8\xbf\x87\xe5\x8d\x83\xe4\xba\xbf\xe7\x9a\x84\xe4\xb8\xad\xe6\x96\x87\xe7\xbd\x91\xe9\xa1\xb5\xe6\x95\xb0\xe6\x8d\xae\xe5\xba\x93\xef\xbc\x8c\xe5\x8f\xaf\xe4\xbb\xa5\xe7\x9e\xac\xe9\x97\xb4\xe6\x89\xbe\xe5\x88\xb0\xe7\x9b\xb8\xe5\x85\xb3\xe7\x9a\x84\xe6\x90\x9c\xe7\xb4\xa2\xe7\xbb\x93\xe6\x9e\x9c\xe3\x80\x82"><link rel="shortcut icon" href="/favicon.ico" rel="external nofollow" type="image/x-icon" /><link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" rel="external nofollow" title="\xe7\x99\xbe\xe5\xba\xa6\xe6\x90\x9c\xe7\xb4\xa2" /><link rel="icon" sizes="any" mask rel="external nofollow" ><link rel="dns-prefetch" rel="external nofollow" /><link rel="dns-prefetch" rel="external nofollow" /><link rel="dns-prefetch" rel="external nofollow" /><link rel="dns-prefetch" rel="external nofollow" /><link rel="dns-prefetch" rel="external nofollow" /><link rel="dns-prefetch" rel="external nofollow" />
2、網絡超時問題
在訪問一個網頁時,如果該網頁長時間未響應,系統(tǒng)就會判斷該網頁超時,而無法打開網頁。下面通過代碼來模擬一個網絡超時的現(xiàn)象。
import requests # 循環(huán)發(fā)送請求50次 for a in range(1, 50): # 捕獲異常 try: # 設置超時為0.5秒 response = requests.get('https://www.baidu.com/', timeout=0.5) # 打印狀態(tài)碼 print(response.status_code) # 捕獲異常 except Exception as e: # 打印異常信息 print('異常'+str(e))
結果:
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200
以上代碼中,模擬進行了50次循環(huán)請求,設置超時時間為0.5秒,在0.5秒內服務器未作出相應視為超時,程序會將超時信息打印在控制臺中。
說起網絡異常信息,requests模塊同樣提供了三種常見的網絡異常類,示例代碼如下:
import requests # 導入requests.exceptions模塊中的三種異常類 from requests.exceptions import ReadTimeout,HTTPError,RequestException # 循環(huán)發(fā)送請求50次 for a in range(1, 50): # 捕獲異常 try: # 設置超時為0.5秒 response = requests.get('https://www.baidu.com/', timeout=0.5) # 打印狀態(tài)碼 print(response.status_code) # 超時異常 except ReadTimeout: print('timeout') # HTTP異常 except HTTPError: print('httperror') # 請求異常 except RequestException: print('reqerror')
結果:
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python編程scoketServer實現(xiàn)多線程同步實例代碼
這篇文章主要介紹了Python編程scoketServer實現(xiàn)多線程同步實例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01Django JWT Token RestfulAPI用戶認證詳解
這篇文章主要介紹了Django JWT Token RestfulAPI用戶認證詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01Pytorch+PyG實現(xiàn)EdgeCNN過程示例詳解
這篇文章主要為大家介紹了Pytorch+PyG實現(xiàn)EdgeCNN過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04python 按照sheet合并多個Excel的示例代碼(多個sheet)
這篇文章主要介紹了python 按照sheet合并多個Excel的示例代碼(多個sheet),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09Python數(shù)據結構與算法之圖的基本實現(xiàn)及迭代器實例詳解
這篇文章主要介紹了Python數(shù)據結構與算法之圖的基本實現(xiàn)及迭代器,結合實例形式詳細分析了數(shù)據結構與算法中圖的實現(xiàn)及迭代器相關算法原理與操作技巧,需要的朋友可以參考下2017-12-12