在使用python爬蟲的時候,經常會遇見所要爬取的網站採取了反爬取技術,高強度、高效率地爬取網頁信息常常會給網站伺服器帶來巨大壓力,所以同一個IP反覆爬取同一個網頁,就很可能被封,那如何解決呢?使用代理ip,設置代理ip池。
私信小編01即可獲取大量Python學習資料
以下介紹的免費獲取代理ip池的方法:
優點:
1.免費
缺點:
1.代理ip穩定性差需要經常更換
2.爬取後ip存在很多不可用ip需要定期篩選
小建議:
該方法比較適合學習使用,如果做項目研究的話建議參考本人博客《python爬蟲設置代理ip池——方法(二)》,購買穩定的代理ip
"""
一.主要思路
1.從代理ip網站爬取IP位址及埠號並儲存
2.驗證ip是否能用
3.格式化ip地址
4.在requests中使用代理ip爬取網站
二. 寫在前面
在Requests中使用代理爬取的格式是
import requests
requests.get(url, headers=headers,proxies=proxies)
其中proxies是一個字典其格式為:
對每個ip都有
proxies = {
http: 'http://114.99.7.122:8752'
https: 'https://114.99.7.122:8752'
}
注意:
對於http和https兩個元素,這裡的http和https
代表的不是代理網站上在ip後面接的類型
代表的是requests訪問的網站的傳輸類型是http還是https
你爬的網站是http類型的你就用http,如果是https類型的你就用https,在代理網站上爬的時候也要分別爬http或https的ip
三.代碼
1.配置環境,導入包
# IP位址取自國內髙匿代理IP網站:http://www.xicidaili.com/nn/# 僅僅爬取首頁IP位址就足夠一般使用from bs4 import BeautifulSoupimport requestsimport random
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}
2.獲取網頁內容函數
def getHTMLText(url,proxies):try:r = requests.get(url,proxies=proxies)r.raise_for_status()r.encoding = r.apparent_encodingexcept:return 0else:return r.text
3.從代理ip網站獲取代理ip列表函數,並檢測可用性,返回ip列表
def get_ip_list(url):web_data = requests.get(url,headers)soup = BeautifulSoup(web_data.text, 'html')ips = soup.find_all('tr')ip_list = []for i in range(1, len(ips)):ip_info = ips[i]tds = ip_info.find_all('td')ip_list.append(tds[1].text + ':' + tds[2].text)#檢測ip可用性,移除不可用ip:(這裡其實總會出問題,你移除的ip可能只是暫時不能用,剩下的ip使用一次後可能之後也未必能用)for ip in ip_list:try:proxy_host = "https://" + ipproxy_temp = {"https": proxy_host}res = urllib.urlopen(url, proxies=proxy_temp).read()except Exception as e:ip_list.remove(ip)continuereturn ip_list
4.從ip池中隨機獲取ip列表
def get_random_ip(ip_list):proxy_list = []for ip in ip_list:proxy_list.append('http://' + ip)proxy_ip = random.choice(proxy_list)proxies = {'http': proxy_ip}return proxies
5.調用代理
if __name__ == '__main__':url = 'http://www.xicidaili.com/nn/'ip_list = get_ip_list(url)proxies = get_random_ip(ip_list)print(proxies