Python實現(xiàn)采集網(wǎng)站ip代理并檢測是否可用
開發(fā)環(huán)境
Python 3.8
Pycharm
模塊使用
requests >>> pip install requests
parsel >>> pip install parsel
代理ip結(jié)構(gòu)
proxies_dict = { "http": "http://" + ip:端口, "https": "http://" + ip:端口, }
代碼實現(xiàn)步驟
1. 導(dǎo)入模塊
# 導(dǎo)入數(shù)據(jù)請求模塊 import requests # 數(shù)據(jù)請求模塊 第三方模塊 pip install requests # 導(dǎo)入 正則表達式模塊 import re # 內(nèi)置模塊 # 導(dǎo)入數(shù)據(jù)解析模塊 import parsel # 數(shù)據(jù)解析模塊 第三方模塊 pip install parsel >>> 這個是scrapy框架核心組件
2. 發(fā)送請求
對于目標網(wǎng)址發(fā)送請求 https://www.kuaidaili.com/free/
url = f'https://www.kuaidaili.com/free/inha/{page}/' # 確定請求url地址 # 用requests模塊里面get 方法 對于url地址發(fā)送請求, 最后用response變量接收返回數(shù)據(jù) response = requests.get(url)
3. 獲取數(shù)據(jù)
獲取服務(wù)器返回響應(yīng)數(shù)據(jù)(網(wǎng)頁源代碼)
print(response.text)
4. 解析數(shù)據(jù)
提取我們想要的數(shù)據(jù)內(nèi)容
解析數(shù)據(jù)方式方法:
- 正則: 可以直接提取字符串?dāng)?shù)據(jù)內(nèi)容
- xpath: 根據(jù)標簽節(jié)點 提取數(shù)據(jù)內(nèi)容
- css選擇器: 根據(jù)標簽屬性提取數(shù)據(jù)內(nèi)容
哪一種方面用那種, 那是喜歡用那種
正則表達式提取數(shù)據(jù)內(nèi)容
正則提取數(shù)據(jù) re.findall() 調(diào)用模塊里面的方法
正則 遇事不決 .*? 可以匹配任意字符(除了換行符\n以外) re.S
ip_list = re.findall('<td data-title="IP">(.*?)</td>', response.text, re.S) port_list = re.findall('<td data-title="PORT">(.*?)</td>', response.text, re.S) print(ip_list) print(port_list)
css選擇器
css選擇器提取數(shù)據(jù) 需要把獲取下來html字符串?dāng)?shù)據(jù)(response.text) 進行轉(zhuǎn)換
# #list > table > tbody > tr > td:nth-child(1) # //*[@id="list"]/table/tbody/tr/td[1] selector = parsel.Selector(response.text) # 把html 字符串?dāng)?shù)據(jù)轉(zhuǎn)成 selector 對象 ip_list = selector.css('#list tbody tr td:nth-child(1)::text').getall() port_list = selector.css('#list tbody tr td:nth-child(2)::text').getall() print(ip_list) print(port_list)
xpath 提取數(shù)據(jù)
selector = parsel.Selector(response.text) # 把html 字符串?dāng)?shù)據(jù)轉(zhuǎn)成 selector 對象 ip_list = selector.xpath('//*[@id="list"]/table/tbody/tr/td[1]/text()').getall() port_list = selector.xpath('//*[@id="list"]/table/tbody/tr/td[2]/text()').getall()
提取ip
for ip, port in zip(ip_list, port_list): # print(ip, port) proxy = ip + ':' + port proxies_dict = { "http": "http://" + proxy, "https": "http://" + proxy, } print(proxies_dict)
5. 檢測ip質(zhì)量
try: response = requests.get(url=url, proxies=proxies_dict, timeout=1) if response.status_code == 200: print('當(dāng)前代理IP: ', proxies_dict, '可以使用') lis_1.append(proxies_dict) except: print('當(dāng)前代理IP: ', proxies_dict, '請求超時, 檢測不合格') print('獲取的代理IP數(shù)量: ', len(lis)) print('獲取可用的IP代理數(shù)量: ', len(lis_1)) print('獲取可用的IP代理: ', lis_1)
總共爬取了150個,最后測試出只有一個是能用的,所以還是付費的好
到此這篇關(guān)于Python實現(xiàn)采集網(wǎng)站ip代理并檢測是否可用的文章就介紹到這了,更多相關(guān)Python采集網(wǎng)站ip代理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python數(shù)據(jù)結(jié)構(gòu)之列表和元組的詳解
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之列表和元組的詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家徹底理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09Python 實現(xiàn)數(shù)據(jù)庫更新腳本的生成方法
下面小編就為大家?guī)硪黄狿ython 實現(xiàn)數(shù)據(jù)庫更新腳本的生成方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07Python實現(xiàn)PS圖像抽象畫風(fēng)效果的方法
這篇文章主要介紹了Python實現(xiàn)PS圖像抽象畫風(fēng)效果的方法,涉及Python基于skimage模塊進行圖像處理的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01PyQt5連接MySQL及QMYSQL driver not loaded錯誤解決
這篇文章主要介紹了PyQt5連接MySQL及QMYSQL driver not loaded錯誤解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04keras實現(xiàn)多種分類網(wǎng)絡(luò)的方式
這篇文章主要介紹了keras實現(xiàn)多種分類網(wǎng)絡(luò)的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06