本文尤爲適合對python爬蟲有必定了解的人羣,可是若是你是小白,也能從文章中獲取一些互聯網的知識html
<font color=blue size=6>背景:</font>
隨着大數據時代來臨,數據對於咱們每一個人來講都愈來愈重要,而互聯網又是最重要、最普遍的數據獲取來源。python
在這個時代下,咱們每一個人都應該掌握必定的從互聯網獲取信息的能力。而python爬蟲,就是咱們從互聯網獲取信息的利器!服務器
當咱們運用python爬蟲從互聯網上獲取信息時,常常的會遇到被封IP的尷尬狀況。多線程
這是因爲咱們的爬蟲佔據了網站太多的服務器資源而又對它幾乎沒有任何益處,因此人家固然不容許你這麼作。
因此咱們須要使用代理來避免這種狀況。python爬蟲
<font color=blue size=6>正文:</font>函數
先講思路,代碼放在下面:
1.請求網頁的內容
2.使用BeautufulSoup4分析網頁內容,提煉出須要的內容
3.程序可自主選擇爬取http仍是https的代理,高匿名仍是透明代理(推薦高匿名),爬取多少頁
4.多線程對爬取下來的ip進行可用性檢測(畢竟是免費的代理,不少都不能用的,須要本身進行檢測)
5.寫入文件(這部分能夠改動)大數據
<font color=blue size=6>思路的代碼分析:</font>網站
經過 requests.get(url=url, headers=header,proxies={"http":"http://xx.xx.xx.xx"}, timeout=5) 使用http協議的ip代理
或者
經過 requests.get(url=url, headers=header,proxies={"https":"https://xx.xx.xx.xx"}, timeout=5)使用https協議的ip代理
咱們須要的是上面截圖中三個箭頭的元素,由於本程序提供http和https的選擇功能,還有匿名度的選擇功能。
若是有須要其它選擇選項好比代理位置或者響應速度什麼的,能夠自行修改,或者能夠聯繫我。url
<font color=blue size=6>代碼:</font>spa
import requests from bs4 import BeautifulSoup import time import threading IP_MODE = 1 # 1. http 2.https NI_MING_MODE = 1 # 1.高匿 2.透明 PAGE = 50 # 默認爬取50頁 url = "" if IP_MODE == 1: url = "http://www.xiladaili.com/http/{}/" elif IP_MODE == 2: url = "http://www.xiladaili.com/https/{}/" else: # 輸入其它數字默認爲http url = "http://www.xiladaili.com/http/{}/" header = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"} def test_http(host): """ 檢測http代理IP是否有效並將有效IP寫入文件 """ ip = {} ip["http"] = "http://" + host # 經過訪問下面這個ip檢測網站來判斷 url = "http://ip.tool.chinaz.com/" # http無效的狀況大體有兩種 1.沒法訪問這個網頁,那麼這個代理不可用 2.能訪問網頁可是沒有達到代理效果,即仍然使用的本身的ip訪問網頁 try: html = requests.get(url=url, headers=header, proxies=ip, timeout=5).text except: return else: soup = BeautifulSoup(html, "lxml") try: real_ip = soup.select_one(".fz24").text except: return if real_ip == host.split(":")[0]: print("有效IP:" + host) with open("xila_http_list.txt", "a") as af: af.write(host + " ") else: return def test_https(host): """ 檢測https代理是否有效,並將有效IP寫入文件 """ ip = {} ip["https"] = "https://" + host url = "https://blog.csdn.net/luoyangIT" # https檢測只能經過檢查是否能訪問https協議的網頁來判斷,如有其它方法則繼續增長 try: html = requests.get(url=url, headers=header, proxies=ip, timeout=5).text except: return else: print("有效IP:" + host) with open("xila_https_list.txt", "a") as af: af.write(host + " ") def main(): """ 主函數,入口 """ for i in range(1, PAGE): # 延時,避免對服務器形成太大負荷,同時在延時時間內檢測代理可用狀況 time.sleep(3) # 請求頁面text html = requests.get(url=url.format(i), headers=header).text soup = BeautifulSoup(html, "lxml") # 分析元素 tr_list = soup.select_one(".fl-table").select_one("tbody").select("tr") # 獲取元素 for td_list in tr_list: # 高匿 if NI_MING_MODE == 1 and "高匿" in td_list.select("td")[2].text: # http if IP_MODE == 1: t = threading.Thread(target=test_http, args=( td_list.select("td")[0].text,)) t.start() # https elif IP_MODE == 2 and test_https(td_list.select("td")[0].text): t = threading.Thread(target=test_https, args=( td_list.select("td")[0].text,)) t.start() # 透明 elif NI_MING_MODE == 2 and "透明" in td_list.select("td")[2].text: # http if IP_MODE == 1: t = threading.Thread(target=test_http, args=( td_list.select("td")[0].text,)) t.start() # https elif IP_MODE == 2: t = threading.Thread(target=test_https, args=( td_list.select("td")[0].text,)) t.start() if __name__ == "__main__": main()
代碼中我已附上大量註釋,但願你們都能看懂,不懂得能夠私信我交流
個人我的公衆號是【程序小員】,歡迎你的關注!
我是落陽,謝謝你的到訪!