在爬取網頁的時候會頻繁requests網站提供數據,但這樣高頻的向主機端不斷提出請求是會被主機端網管人員針對性封鎖IP,這時候因應的辦法就是更換IP,避免爬行網站時被封鎖IP,可以參考以下的做法 導入需要使用模塊 import requests from requests_toolbelt.adapters import source 程式寫法 s = requests.Session() #使用會話方法儲存cookies ip = 'xxx.xxx.xxx.xxx' new_source = source.SourceAddressAdapter(ip) #調用搭配器的方法 #IP安裝在不同的通訊協定上,會自動調配適合的協定上面再做get or post的動作 s.mount('http://', new_source) s.mount('https://', new_source)
Leave a Comment