溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python怎么創(chuàng)建屬于自己的IP池

發(fā)布時(shí)間:2022-04-15 09:08:10 來源:億速云 閱讀:194 作者:iii 欄目:開發(fā)技術(shù)

這篇“Python怎么創(chuàng)建屬于自己的IP池”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Python怎么創(chuàng)建屬于自己的IP池”文章吧。

開發(fā)環(huán)境

Python 3.8

Pycharm

模塊使用

requests >>> pip install requests

parsel >>> pip install parsel

如果安裝python第三方模塊

win + R 輸入 cmd 點(diǎn)擊確定, 輸入安裝命令 pip install 模塊名 (pip install requests) 回車

在pycharm中點(diǎn)擊Terminal(終端) 輸入安裝命令

如何配置pycharm里面的python解釋器

選擇file(文件) >>> setting(設(shè)置) >>> Project(項(xiàng)目) >>> python interpreter(python解釋器)

點(diǎn)擊齒輪, 選擇add

添加python安裝路徑

pycharm如何安裝插件

選擇file(文件) >>> setting(設(shè)置) >>> Plugins(插件)

點(diǎn)擊 Marketplace 輸入想要安裝的插件名字 比如:翻譯插件 輸入 translation / 漢化插件 輸入 Chinese

選擇相應(yīng)的插件點(diǎn)擊 install(安裝) 即可

安裝成功之后 是會彈出 重啟pycharm的選項(xiàng) 點(diǎn)擊確定, 重啟即可生效

代理ip結(jié)構(gòu)

proxies_dict = {
    "http": "http://" + ip:端口,
    "https": "http://" + ip:端口,
}

思路

一. 數(shù)據(jù)來源分析

找我們想要數(shù)據(jù)內(nèi)容, 從哪里來的

二. 代碼實(shí)現(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)入 正則表達(dá)式模塊
import re  # 內(nèi)置模塊
# 導(dǎo)入數(shù)據(jù)解析模塊
import parsel  # 數(shù)據(jù)解析模塊 第三方模塊 pip install parsel  >>> 這個(gè)是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變量接收返回?cái)?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ù) 進(jìn)行轉(zhuǎn)換
        xpath: 根據(jù)標(biāo)簽節(jié)點(diǎn) 提取數(shù)據(jù)內(nèi)容
        css選擇器: 根據(jù)標(biāo)簽屬性提取數(shù)據(jù)內(nèi)容 
        
        哪一種方面用那種, 那是喜歡用那種
    """
    # 正則表達(dá)式提取數(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) 進(jìn)行轉(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,  '請求超時(shí), 檢測不合格')



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池

以上就是關(guān)于“Python怎么創(chuàng)建屬于自己的IP池”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI