一文教你Python如何創(chuàng)建屬于自己的IP池
開發(fā)環(huán)境
Python 3.8
Pycharm
模塊使用
requests >>> pip install requests
parsel >>> pip install parsel
如果安裝python第三方模塊
win + R 輸入 cmd 點擊確定, 輸入安裝命令 pip install 模塊名 (pip install requests) 回車
在pycharm中點擊Terminal(終端) 輸入安裝命令
如何配置pycharm里面的python解釋器
選擇file(文件) >>> setting(設(shè)置) >>> Project(項目) >>> python interpreter(python解釋器)
點擊齒輪, 選擇add
添加python安裝路徑
pycharm如何安裝插件
選擇file(文件) >>> setting(設(shè)置) >>> Plugins(插件)
點擊 Marketplace 輸入想要安裝的插件名字 比如:翻譯插件 輸入 translation / 漢化插件 輸入 Chinese
選擇相應(yīng)的插件點擊 install(安裝) 即可
安裝成功之后 是會彈出 重啟pycharm的選項 點擊確定, 重啟即可生效
代理ip結(jié)構(gòu)
proxies_dict = {
"http": "http://" + ip:端口,
"https": "http://" + ip:端口,
}
思路
一. 數(shù)據(jù)來源分析
找我們想要數(shù)據(jù)內(nèi)容, 從哪里來的
二. 代碼實現(xiàn)步驟
發(fā)送請求, 對于目標(biāo)網(wǎng)址發(fā)送請求
獲取數(shù)據(jù), 獲取服務(wù)器返回響應(yīng)數(shù)據(jù)(網(wǎng)頁源代碼)
解析數(shù)據(jù), 提取我們想要的數(shù)據(jù)內(nèi)容
保存數(shù)據(jù), 爬音樂 視頻 本地csv 數(shù)據(jù)庫… IP檢測, 檢測IP代理是否可用 可用用IP代理 保存
- from 從
- import 導(dǎo)入
- 從 什么模塊里面 導(dǎo)入 什么方法
- from xxx import * # 導(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框架核心組件
lis = []
lis_1 = []
# 1. 發(fā)送請求, 對于目標(biāo)網(wǎng)址發(fā)送請求 https://www.kuaidaili.com/free/
for page in range(11, 21):
url = f'https://www.kuaidaili.com/free/inha/{page}/' # 確定請求url地址
"""
headers 請求頭 作用偽裝python代碼
"""
# 用requests模塊里面get 方法 對于url地址發(fā)送請求, 最后用response變量接收返回數(shù)據(jù)
response = requests.get(url)
# <Response [200]> 請求之后返回response響應(yīng)對象, 200狀態(tài)碼表示請求成功
# 2. 獲取數(shù)據(jù), 獲取服務(wù)器返回響應(yīng)數(shù)據(jù)(網(wǎng)頁源代碼) response.text 獲取響應(yīng)體文本數(shù)據(jù)
# print(response.text)
# 3. 解析數(shù)據(jù), 提取我們想要的數(shù)據(jù)內(nèi)容
"""
解析數(shù)據(jù)方式方法:
正則: 可以直接提取字符串?dāng)?shù)據(jù)內(nèi)容
需要把獲取下來html字符串?dāng)?shù)據(jù) 進行轉(zhuǎn)換
xpath: 根據(jù)標(biāo)簽節(jié)點 提取數(shù)據(jù)內(nèi)容
css選擇器: 根據(jù)標(biāo)簽屬性提取數(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)換
# 我不會css 或者 xpath 怎么辦
# #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()
# print(ip_list)
# print(port_list)
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)
lis.append(proxies_dict)
# 4.檢測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)
dit = {
'http': 'http://110.189.152.86:40698',
'https': 'http://110.189.152.86:40698'
}


到此這篇關(guān)于一文教你Python如何創(chuàng)建屬于自己的IP池的文章就介紹到這了,更多相關(guān)Python創(chuàng)建IP池內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python數(shù)據(jù)結(jié)構(gòu)與算法中的隊列詳解(2)
這篇文章主要為大家詳細介紹了Python中的隊列,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
python實現(xiàn)輸入的數(shù)據(jù)在地圖上生成熱力圖效果
今天小編就為大家分享一篇python實現(xiàn)輸入的數(shù)據(jù)在地圖上生成熱力圖效果,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
python學(xué)習(xí)字符串駐留與常量折疊隱藏特性詳解
這篇文章主要為大家介紹了python學(xué)習(xí)中字符串駐留與常量折疊的一些隱藏特性,并給大家進行了詳細分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09

