這篇文章主要介紹了java如何建立爬蟲代理ip池,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網(wǎng)營(yíng)銷、網(wǎng)站重做改版、紅崗網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、html5、商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為紅崗等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
步驟說(shuō)明
1、要從提供代理ip的網(wǎng)站上抓取ip。
2、初步過(guò)濾捕獲的ip。例如,從一開始就過(guò)濾IP類型不是HTTPS,IP鏈接速度超過(guò)2秒。
3、對(duì)于符合要求的IP,進(jìn)行質(zhì)量檢查,判斷是否可用。這一步是檢查IP的質(zhì)量,也就是說(shuō)這一步刷掉了很多IP。
4、將符合要求的ip寫入redis數(shù)據(jù)庫(kù),并以List形式存儲(chǔ)在Redis中。
5、設(shè)置一個(gè)抓取周期來(lái)更新您的IP代理池(抓取并處理新的IP后,我們清空原始數(shù)據(jù)庫(kù),并將新的IP寫入其中)
實(shí)例
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import requests,threading,datetime from bs4 import BeautifulSoup import random """ 1、抓取代理網(wǎng)站的代理ip 2、并根據(jù)指定的目標(biāo)url,對(duì)抓取到ip的有效性進(jìn)行驗(yàn)證 3、最后存到指定的path """ # ------------------------------------------------------文檔處理-------------------------------------------------------- # 寫入文檔 def write(path,text): with open(path,'a', encoding='utf-8') as f: f.writelines(text) f.write('\n') # 清空文檔 def truncatefile(path): with open(path, 'w', encoding='utf-8') as f: f.truncate() # 讀取文檔 def read(path): with open(path, 'r', encoding='utf-8') as f: txt = [] for s in f.readlines(): txt.append(s.strip()) return txt # ---------------------------------------------------------------------------------------------------------------------- # 計(jì)算時(shí)間差,格式: 時(shí)分秒 def gettimediff(start,end): seconds = (end - start).seconds m, s = divmod(seconds, 60) h, m = divmod(m, 60) diff = ("%02d:%02d:%02d" % (h, m, s)) return diff # ---------------------------------------------------------------------------------------------------------------------- # 返回一個(gè)隨機(jī)的請(qǐng)求頭 headers def getheaders(): user_agent_list = [ \ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1" \ "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11", \ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6", \ "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6", \ "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1", \ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5", \ "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5", \ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", \ "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", \ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", \ "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", \ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", \ "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", \ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", \ "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", \ "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3", \ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24", \ "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24" ] UserAgent=random.choice(user_agent_list) headers = {'User-Agent': UserAgent} return headers # -----------------------------------------------------檢查ip是否可用---------------------------------------------------- def checkip(targeturl,ip): headers =getheaders() # 定制請(qǐng)求頭 proxies = {"http": "http://"+ip, "https": "http://"+ip} # 代理ip try: response=requests.get(url=targeturl,proxies=proxies,headers=headers,timeout=5).status_code if response == 200 : return True else: return False except: return False #-------------------------------------------------------獲取代理方法---------------------------------------------------- # 免費(fèi)代理 XiciDaili def findip(type,pagenum,targeturl,path): # ip類型,頁(yè)碼,目標(biāo)url,存放ip的路徑 list={'1': 'http://www.xicidaili.com/nt/', # xicidaili國(guó)內(nèi)普通代理 '2': 'http://www.xicidaili.com/nn/', # xicidaili國(guó)內(nèi)高匿代理 '3': 'http://www.xicidaili.com/wn/', # xicidaili國(guó)內(nèi)https代理 '4': 'http://www.xicidaili.com/wt/'} # xicidaili國(guó)外http代理 url=list[str(type)]+str(pagenum) # 配置url headers = getheaders() # 定制請(qǐng)求頭 html=requests.get(url=url,headers=headers,timeout = 5).text soup=BeautifulSoup(html,'lxml') all=soup.find_all('tr',class_='odd') for i in all: t=i.find_all('td') ip=t[1].text+':'+t[2].text is_avail = checkip(targeturl,ip) if is_avail == True: write(path=path,text=ip) print(ip) #-----------------------------------------------------多線程抓取ip入口--------------------------------------------------- def getip(targeturl,path): truncatefile(path) # 爬取前清空文檔 start = datetime.datetime.now() # 開始時(shí)間 threads=[] for type in range(4): # 四種類型ip,每種類型取前三頁(yè),共12條線程 for pagenum in range(3): t=threading.Thread(target=findip,args=(type+1,pagenum+1,targeturl,path)) threads.append(t) print('開始爬取代理ip') for s in threads: # 開啟多線程爬取 s.start() for e in threads: # 等待所有線程結(jié)束 e.join() print('爬取完成') end = datetime.datetime.now() # 結(jié)束時(shí)間 diff = gettimediff(start, end) # 計(jì)算耗時(shí) ips = read(path) # 讀取爬到的ip數(shù)量 print('一共爬取代理ip: %s 個(gè),共耗時(shí): %s \n' % (len(ips), diff)) #-------------------------------------------------------啟動(dòng)----------------------------------------------------------- if __name__ == '__main__': path = 'ip.txt' # 存放爬取ip的文檔path targeturl = 'http://www.cnblogs.com/TurboWay/' # 驗(yàn)證ip有效性的指定url getip(targeturl,path)
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“java如何建立爬蟲代理ip池”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
網(wǎng)站名稱:java如何建立爬蟲代理ip池
文章起源:http://sd-ha.com/article4/jgijie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、微信小程序、云服務(wù)器、小程序開發(fā)、外貿(mào)建站、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)