python爬取代理ip的示例
要寫爬蟲爬取大量的數(shù)據(jù),就會(huì)面臨ip被封的問題,雖然可以通過設(shè)置延時(shí)的方法來延緩對(duì)網(wǎng)站的訪問,但是一旦訪問次數(shù)過多仍然會(huì)面臨ip被封的風(fēng)險(xiǎn),這時(shí)我們就需要用到動(dòng)態(tài)的ip地址來隱藏真實(shí)的ip信息,如果做爬蟲項(xiàng)目,建議選取一些平臺(tái)提供的動(dòng)態(tài)ip服務(wù),引用api即可。目前國內(nèi)有很多提供動(dòng)態(tài)ip的平臺(tái),普遍價(jià)格不菲,而對(duì)于只想跑個(gè)小項(xiàng)目用來學(xué)習(xí)的話可以參考下本篇文章。
簡述本篇使用簡單的爬蟲程序來爬取免費(fèi)ip網(wǎng)站的ip信息并生成json文檔,存儲(chǔ)可用的ip地址,寫其它爬取項(xiàng)目的時(shí)候可以從生成的json文檔中提取ip地址使用,為了確保使用的ip地址的有效性,建議對(duì)json文檔中的ip現(xiàn)爬現(xiàn)用,并且在爬取時(shí)對(duì)ip有效性的時(shí)間進(jìn)行篩選,只爬取時(shí)長較長、可用的ip地址存儲(chǔ)。
實(shí)現(xiàn)使用平臺(tái)https://www.xicidaili.com/nn/來作為數(shù)據(jù)源,通過對(duì)http://www.baidu.com/的相應(yīng)來判斷ip的可使用性。引用lxml模塊來對(duì)網(wǎng)頁數(shù)據(jù)進(jìn)行提取,當(dāng)然也可以使用re模塊來進(jìn)行匹配提取,這里只使用lxml模塊對(duì)數(shù)據(jù)進(jìn)行提取。訪問https://www.xicidaili.com/nn/數(shù)據(jù)源,并且啟動(dòng)Fiddler對(duì)瀏覽器數(shù)據(jù)進(jìn)行監(jiān)聽,我這里瀏覽器采用的是Proxy SwitchyOmega插件來配合Fiddler進(jìn)行使用,在Fiddler找到/nn/*數(shù)據(jù)查看User-Agent信息并復(fù)制下來作為我們?cè)L問的頭文件。如圖:
引入模塊
import requestsfrom lxml import etreeimport timeimport json
獲取所有數(shù)據(jù)
def get_all_proxy(page): url = ’https://www.xicidaili.com/nn/%s’%page headers = { ’User-Agent’: ’Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36’, } response = requests.get(url, headers=headers) html_ele = etree.HTML(response.text) ip_eles = html_ele.xpath(’//table[@id='ip_list']/tr/td[2]/text()’) port_ele = html_ele.xpath(’//table[@id='ip_list']/tr/td[3]/text()’) print(ip_eles) proxy_list = [] for i in range(0,len(ip_eles)): check_all_proxy(ip_eles[i],port_ele[i]) return proxy_list
對(duì)數(shù)據(jù)進(jìn)行篩選:
def check_all_proxy(host,port): type = ’http’ proxies = {} proxy_str = '%s://@%s:%s' % (type, host, port) valid_proxy_list = [] url = ’http://www.baidu.com/’ proxy_dict = { ’http’: proxy_str, ’https’: proxy_str } try: start_time = time.time() response = requests.get(url, proxies=proxy_dict, timeout=5) if response.status_code == 200:end_time = time.time()print(’代理可用:’ + proxy_str)print(’耗時(shí):’ + str(end_time - start_time))proxies[’type’] = typeproxies[’host’] = hostproxies[’port’] = portproxiesJson = json.dumps(proxies)with open(’verified_y.json’, ’a+’) as f: f.write(proxiesJson + ’n’)print('已寫入:%s' % proxy_str)valid_proxy_list.append(proxy_str) else:print(’代理超時(shí)’) except: print(’代理不可用--------------->’+proxy_str)
運(yùn)行程序:
if __name__ == ’__main__’: for i in range(1,11): #選取前十頁數(shù)據(jù)使用 proxy_list = get_all_proxy(i) time.sleep(20) print(valid_proxy_list)
生成的json文件:
以上就是python爬取代理ip的示例的詳細(xì)內(nèi)容,更多關(guān)于python爬取代理ip的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. ASP基礎(chǔ)入門第二篇(ASP基礎(chǔ)知識(shí))2. 不使用XMLHttpRequest對(duì)象實(shí)現(xiàn)Ajax效果的方法小結(jié)3. python使用pywinauto驅(qū)動(dòng)微信客戶端實(shí)現(xiàn)公眾號(hào)爬蟲4. Python如何解決secure_filename對(duì)中文不支持問題5. ThinkPHP6使用JWT+中間件實(shí)現(xiàn)Token驗(yàn)證實(shí)例詳解6. python使用ProjectQ生成量子算法指令集7. 怎樣打開XML文件?xml文件如何打開?8. Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考9. python 定義函數(shù) 返回值只取其中一個(gè)的實(shí)現(xiàn)10. JSP出現(xiàn)中文亂碼問題解決方法詳解
