一文教你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(設置) >>> Project(項目) >>> python interpreter(python解釋器)
點擊齒輪, 選擇add
添加python安裝路徑
pycharm如何安裝插件
選擇file(文件) >>> setting(設置) >>> Plugins(插件)
點擊 Marketplace 輸入想要安裝的插件名字 比如:翻譯插件 輸入 translation / 漢化插件 輸入 Chinese
選擇相應的插件點擊 install(安裝) 即可
安裝成功之后 是會彈出 重啟pycharm的選項 點擊確定, 重啟即可生效
代理ip結構
proxies_dict = {
"http": "http://" + ip:端口,
"https": "http://" + ip:端口,
}
思路
一. 數(shù)據(jù)來源分析
找我們想要數(shù)據(jù)內(nèi)容, 從哪里來的
二. 代碼實現(xiàn)步驟
發(fā)送請求, 對于目標網(wǎng)址發(fā)送請求
獲取數(shù)據(jù), 獲取服務器返回響應數(shù)據(jù)(網(wǎng)頁源代碼)
解析數(shù)據(jù), 提取我們想要的數(shù)據(jù)內(nèi)容
保存數(shù)據(jù), 爬音樂 視頻 本地csv 數(shù)據(jù)庫… IP檢測, 檢測IP代理是否可用 可用用IP代理 保存
- from 從
- import 導入
- 從 什么模塊里面 導入 什么方法
- from xxx import * # 導入所有方法
代碼
# 導入數(shù)據(jù)請求模塊
import requests # 數(shù)據(jù)請求模塊 第三方模塊 pip install requests
# 導入 正則表達式模塊
import re # 內(nèi)置模塊
# 導入數(shù)據(jù)解析模塊
import parsel # 數(shù)據(jù)解析模塊 第三方模塊 pip install parsel >>> 這個是scrapy框架核心組件
lis = []
lis_1 = []
# 1. 發(fā)送請求, 對于目標網(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響應對象, 200狀態(tài)碼表示請求成功
# 2. 獲取數(shù)據(jù), 獲取服務器返回響應數(shù)據(jù)(網(wǎng)頁源代碼) response.text 獲取響應體文本數(shù)據(jù)
# print(response.text)
# 3. 解析數(shù)據(jù), 提取我們想要的數(shù)據(jù)內(nèi)容
"""
解析數(shù)據(jù)方式方法:
正則: 可以直接提取字符串數(shù)據(jù)內(nèi)容
需要把獲取下來html字符串數(shù)據(jù) 進行轉換
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字符串數(shù)據(jù)(response.text) 進行轉換
# 我不會css 或者 xpath 怎么辦
# #list > table > tbody > tr > td:nth-child(1)
# //*[@id="list"]/table/tbody/tr/td[1]
selector = parsel.Selector(response.text) # 把html 字符串數(shù)據(jù)轉成 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 字符串數(shù)據(jù)轉成 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('當前代理IP: ', proxies_dict, '可以使用')
lis_1.append(proxies_dict)
except:
print('當前代理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'
}


到此這篇關于一文教你Python如何創(chuàng)建屬于自己的IP池的文章就介紹到這了,更多相關Python創(chuàng)建IP池內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Django+vue+vscode前后端分離搭建的實現(xiàn)
本文以一個非常簡單的demo為例,介紹了利用django+drf+vue的前后端分離開發(fā)模式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2023-08-08
Python利用pandas進行數(shù)據(jù)合并詳解
當使用Python中的pandas庫時,merge函數(shù)是用于合并(或連接)兩個數(shù)據(jù)框(DataFrame)的重要工具。它類似于SQL中的JOIN操作,下面我們就來看看它的具體操作吧2023-11-11
Django Rest framework之認證的實現(xiàn)代碼
這篇文章主要介紹了Django Rest framework之認證的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12

