Python實現(xiàn)IP代理批量采集的示例代碼
開發(fā)環(huán)境
python 3.8
pycharm
模塊使用
import requests —> 需要安裝 pip install requests
import parsel —> 需要安裝 pip install parsel 解析數(shù)據(jù)模塊
如果安裝python第三方模塊:
- win + R 輸入 cmd 點擊確定, 輸入安裝命令 pip install 模塊名 (pip install requests) 回車
- 在pycharm中點擊Terminal(終端) 輸入安裝命令
IP代理: 采集網(wǎng)站數(shù)據(jù), 采集比較快, 你被封IP <一段時間內(nèi)容 不能訪問這個網(wǎng)站>
基本流程(思路)
一. 數(shù)據(jù)來源分析
你要先分析, 你想要數(shù)據(jù)是請求那個url地址可以得到…
通過開發(fā)者工具抓包分析, 分析我們想要數(shù)據(jù)來源
I. F12或者鼠標右鍵點檢查 選擇network 刷新網(wǎng)頁
II. 分析數(shù)據(jù)內(nèi)容 <IP 以及 端口>來自于哪里
通過開發(fā)者工具 關鍵字搜索數(shù)據(jù)來源 找到相對應的數(shù)據(jù)包
二. 代碼實現(xiàn)步驟過程
爬蟲基本四大步驟
發(fā)送請求, 模擬瀏覽器對于分析得到url地址發(fā)送請求 https://free.kuaidaili.com/free/inha/1/
獲取數(shù)據(jù), 獲取服務器返回響應數(shù)據(jù) —> 開發(fā)者工具里面看到 response
解析數(shù)據(jù), 提取我們想要數(shù)據(jù)內(nèi)容
保存數(shù)據(jù), 我們想要數(shù)據(jù)內(nèi)容保存本地
代碼
# 導入數(shù)據(jù)請求模塊 ---> 第三方模塊, 需要安裝 在cmd里面 pip install requests import requests # 導入數(shù)據(jù)解析模塊 ---> 第三方模塊, 需要安裝 在cmd里面 pip install parsel import parsel # 導入json模塊 ---> 內(nèi)置模塊 不需要安裝 import json
# 1. 發(fā)送請求, 模擬瀏覽器對于分析得到url地址發(fā)送請求 proxies_list = [] proxies_list_1 = [] # 請求url地址 for page in range(1, 11): url = f'https://www.boc.cn/sourcedb/whpj/index_{page}.html' """ headers請求頭, 模擬偽裝瀏覽器去發(fā)送請求 不加headers相當于裸奔 ----> 告訴服務器, 我是爬蟲 我是爬蟲~ 你來抓我~ 加什么東西, 在哪加 ---> 開發(fā)者工具里面 復制 ua """ headers = { # User-Agent 用戶代理 表示瀏覽器基本身份標識 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36' } # 發(fā)送請求 ---> <Response [200]> 響應對象, 200狀態(tài)碼 表示請求成功 response = requests.get(url=url, headers=headers) # 2. 獲取數(shù)據(jù), 獲取服務器返回響應數(shù)據(jù) print(response.text) """ 3. 解析數(shù)據(jù), 提取我們想要數(shù)據(jù)內(nèi)容 解析方法: re正則: 對于字符串數(shù)據(jù)進行提取 css: 根據(jù)標簽屬性內(nèi)容提取 xpath: 根據(jù)標簽節(jié)點提取 """ # 轉(zhuǎn)換數(shù)據(jù)類型 response.text<字符串數(shù)據(jù)> <Selector xpath=None data='<html>\n<head>\n<meta http-equiv="X-UA-...'> selector = parsel.Selector(response.text) # 獲取tr標簽 ---> 返回列表 列表里面元素是 Selector對象 trs = selector.css('#list table tbody tr') trs_1 = selector.xpath('//*[@id="list"]/table/tbody/tr') # for循環(huán) 一個一個提取tr標簽 for tr in trs: # 提取ip號 td:nth-child(1)::text 獲取第一個td標簽里面文本數(shù)據(jù) ip_num = tr.css('td:nth-child(1)::text').get() # ip_num_1 = tr.xpath('td[1]/text()').get() ip_port = tr.css('td:nth-child(2)::text').get() """ IP代理結(jié)構(gòu)是什么樣子的? proxies_dict = { "http": "http://" + ip:端口, "https": "http://" + ip:端口, } """ proxies_dict = { "http": "http://" + ip_num + ':' + ip_port, "https": "https://" + ip_num + ':' + ip_port, } proxies_list_1.append(proxies_dict) # 檢測IP代理是否可用 用這個代理去請求一下網(wǎng)站就好了 try: response_1 = requests.get(url='https://www.baidu.com/', proxies=proxies_dict, timeout=1) if response_1.status_code == 200: proxies_list.append(proxies_dict) print('代理可以使用: ', proxies_dict) # 保存代理到文本 with open('代理.txt', mode='a', encoding='utf-8') as f: f.write(json.dumps(proxies_dict)) f.write('\n') except: print('當前代理:', proxies_dict, '請求超時, 檢測不合格') print('===' * 50) print('一共獲取到:', len(proxies_list_1)) print('可以使用代理: ', len(proxies_list)) print(proxies_list)
到此這篇關于Python實現(xiàn)IP代理批量采集的示例代碼的文章就介紹到這了,更多相關Python采集IP代理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Opencv中cv2.cvtColor彩色圖轉(zhuǎn)灰度圖的其他6種方法
本文主要介紹了Opencv中cv2.cvtColor彩色圖轉(zhuǎn)灰度圖的其他6種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05查找python項目依賴并生成requirements.txt的方法
今天小編就為大家分享一篇查找python項目依賴并生成requirements.txt的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07使用python實現(xiàn)遞歸版漢諾塔示例(漢諾塔遞歸算法)
這篇文章主要介紹了使用python實現(xiàn)遞歸版漢諾塔示例(漢諾塔遞歸算法),需要的朋友可以參考下2014-04-04Python Pandas pandas.read_sql函數(shù)實例用法
在本篇文章里小編給大家整理的是一篇關于Python Pandas pandas.read_sql函數(shù)詳解內(nèi)容,有需要的朋友們可以學習下。2021-06-06Python數(shù)學建模學習模擬退火算法多變量函數(shù)優(yōu)化示例解析
模擬退火算法借鑒了統(tǒng)計物理學的思想,是一種簡單、通用的啟發(fā)式優(yōu)化算法,并在理論上具有概率性全局優(yōu)化性能,因而在科研和工程中得到了廣泛的應用2021-10-10