Python调用ip代理案例,demo,参考官方给出的案例,整理,这里使用的是快代理api,可以实现ip代理的更换,主要整理了requests的调用以及无头浏览器 Chrome的使用,同时使用了站长之家ip检测的api工具!

Python调用ip代理案例,快代理api调用demo_Python开发

Python调用ip代理案例,快代理api调用demo_Python开发_02

以下代码仅供参考,欢迎交流!

requests(推荐)官方demo

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
使用requests请求代理服务器
请求http和https网页均适用
"""

import requests

# 提取代理API接口,获取1个代理IP
api_url = "http://dps.kdlapi.com/api/getdps/?orderid=9266892014xxxxx&num=1&pt=1&sep=1"

# 获取API接口返回的代理IP
proxy_ip = requests.get(api_url).text

# 用户名密码认证(私密代理/独享代理)
username = "username"
password = "password"
proxies = {
    "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip},
    "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip}
}

# 白名单方式(需提前设置白名单)
# proxies = {
#     "http": "http://%(proxy)s/" % {"proxy": proxy_ip},
#     "https": "http://%(proxy)s/" % {"proxy": proxy_ip}
# }

# 要访问的目标网页
target_url = "https://dev.kdlapi.com/testproxy"

# 使用代理IP发送请求
response = requests.get(target_url, proxies=proxies)

# 获取页面内容
if response.status_code == 200:
    print(response.text)

requests 实际调用整理 demo

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#author 微信:huguo00289

#获取代理ip
def get_ips(orderid,username,password):
    """
    使用requests请求代理服务器
    请求http和https网页均适用
    """
    api_url = f"http://dps.kdlapi.com/api/getdps/?orderid={orderid}&num=1&pt=1&sep=2"
    # 获取API接口返回的代理IP
    proxy_ip = requests.get(api_url).text
    proxy_ip=proxy_ip.replace('\n','')
    print(f"正在使用代理ip为: {proxy_ip}")
    #  用户名密码认证(私密代理/独享代理)
    proxies = {
        "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip},
        "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip}
    }
    # 要访问的目标网页
    target_url = "https://dev.kdlapi.com/testproxy"

    # 使用代理IP发送请求
    response = requests.get(target_url, proxies=proxies)

    # 获取页面内容
    if response.status_code == 200:
        print(response.text)

    return proxy_ip


#检测代理ip
def test_ip(proxy_ip,key):
    """
        站在工具api查询ip地址信息
    """
    ip=proxy_ip.split(':')[0]
    url=f'http://apidata.chinaz.com/CallAPI/ip?key={key}&ip={ip}'
    response = requests.get(url)
    req=json.loads(response.text)
    print(f"验证ip地址:{req['Reason']}")
    print(f"ip地址信息为{req['Result']['Province']}-{req['Result']['City']}")
    
    return proxy_ip



if __name__=="__main__":
    orderid="orderid"
    username = "username"
    password = "password"
    key="key"
    proxy_ip=get_ips(orderid,username,password)
    test_ip(proxy_ip, key)
    


运行效果

Python调用ip代理案例,快代理api调用demo_Python开发_03

无头浏览器 Chrome(用户名密码认证)官方 demo

#!/usr/bin/env python
# encoding: utf-8

from selenium import webdriver
import string
import zipfile
import time


def create_proxyauth_extension(proxy_host, proxy_port, proxy_username, proxy_password, scheme='http', plugin_path=None):
    """代理认证插件

    args:
        proxy_host (str): 你的代理地址或者域名(str类型)
        proxy_port (int): 代理端口号(int类型)
        # 用户名密码认证(私密代理/独享代理)
        proxy_username (str):用户名(字符串)
        proxy_password (str): 密码 (字符串)
    kwargs:
        scheme (str): 代理方式 默认http
        plugin_path (str): 扩展的绝对路径

    return str -> plugin_path
    """

    if plugin_path is None:
        plugin_path = 'vimm_chrome_proxyauth_plugin.zip'

    manifest_json = """
    {
        "version": "1.0.0",
        "manifest_version": 2,
        "name": "Chrome Proxy",
        "permissions": [
            "proxy",
            "tabs",
            "unlimitedStorage",
            "storage",
            "<all_urls>",
            "webRequest",
            "webRequestBlocking"
        ],
        "background": {
            "scripts": ["background.js"]
        },
        "minimum_chrome_version":"22.0.0"
    }
    """

    background_js = string.Template(
        """
        var config = {
                mode: "fixed_servers",
                rules: {
                singleProxy: {
                    scheme: "${scheme}",
                    host: "${host}",
                    port: parseInt(${port})
                },
                bypassList: ["foobar.com"]
                }
            };

        chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

        function callbackFn(details) {
            return {
                authCredentials: {
                    username: "${username}",
                    password: "${password}"
                }
            };
        }

        chrome.webRequest.onAuthRequired.addListener(
                    callbackFn,
                    {urls: ["<all_urls>"]},
                    ['blocking']
        );
        """
    ).substitute(
        host=proxy_host,
        port=proxy_port,
        username=proxy_username,
        password=proxy_password,
        scheme=scheme,
    )
    with zipfile.ZipFile(plugin_path, 'w') as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_js)
    return plugin_path


proxyauth_plugin_path = create_proxyauth_extension(
    proxy_host="${proxy_ip}",  # 代理IP
    proxy_port="${proxy_port}",  # 端口号
    # 用户名密码(私密代理/独享代理)
    proxy_username="${username}", 
    proxy_password="${password}"
)


options = webdriver.ChromeOptions()
options.add_extension(proxyauth_plugin_path)
# ${chromedriver_path}: chromedriver驱动存放路径
driver = webdriver.Chrome(executable_path="${chromedriver_path}", options=options)
driver.get("https://dev.kdlapi.com/testproxy")

# 获取页面内容
print(driver.page_source)

# 延迟3秒后关闭当前窗口,如果是最后一个窗口则退出
time.sleep(3)
driver.close()

无头浏览器 Chrome(用户名密码认证)实际调用整理 demo

#!/usr/bin/env python
# encoding: utf-8
# author 微信:huguo00289

from selenium import webdriver
import string
import zipfile
import time
import requests

#获取代理ip
def get_ips(orderid):
    """
    使用requests请求代理服务器
    请求http和https网页均适用
    """
    api_url = f"http://dps.kdlapi.com/api/getdps/?orderid={orderid}&num=1&pt=1&sep=2"
    # 获取API接口返回的代理IP
    proxy= requests.get(api_url).text
    proxy=proxy.replace('\n','')
    print(f"正在使用代理ip为: {proxy}")
    proxy = proxy.split(':')
    return proxy




def create_proxyauth_extension(proxy_ip, proxy_port, username, password, scheme='http', plugin_path=None):
    """代理认证插件

    args:
        proxy_host (str): 你的代理地址或者域名(str类型)
        proxy_port (int): 代理端口号(int类型)
        # 用户名密码认证(私密代理/独享代理)
        proxy_username (str):用户名(字符串)
        proxy_password (str): 密码 (字符串)
    kwargs:
        scheme (str): 代理方式 默认http
        plugin_path (str): 扩展的绝对路径

    return str -> plugin_path
    """

    if plugin_path is None:
        plugin_path = 'vimm_chrome_proxyauth_plugin.zip'

    manifest_json = """
    {
        "version": "1.0.0",
        "manifest_version": 2,
        "name": "Chrome Proxy",
        "permissions": [
            "proxy",
            "tabs",
            "unlimitedStorage",
            "storage",
            "<all_urls>",
            "webRequest",
            "webRequestBlocking"
        ],
        "background": {
            "scripts": ["background.js"]
        },
        "minimum_chrome_version":"22.0.0"
    }
    """

    background_js = string.Template(
        """
        var config = {
                mode: "fixed_servers",
                rules: {
                singleProxy: {
                    scheme: "${scheme}",
                    host: "${host}",
                    port: parseInt(${port})
                },
                bypassList: ["foobar.com"]
                }
            };

        chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

        function callbackFn(details) {
            return {
                authCredentials: {
                    username: "${username}",
                    password: "${password}"
                }
            };
        }

        chrome.webRequest.onAuthRequired.addListener(
                    callbackFn,
                    {urls: ["<all_urls>"]},
                    ['blocking']
        );
        """
    ).substitute(
        host=proxy_ip,
        port=proxy_port,
        username=username,
        password=password,
        scheme=scheme,
    )
    with zipfile.ZipFile(plugin_path, 'w') as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_js)

    return plugin_path







if __name__=="__main__":
    orderid=orderid
    username = username
    password = password
    proxy=get_ips(orderid)
    proxy_ip=proxy[0]
    proxy_port=proxy[1]
    print(proxy_ip,proxy_port)
    proxyauth_plugin_path = create_proxyauth_extension(
        proxy_ip,  # 代理IP
        proxy_port,  # 端口号
        username,
        password,
    )

    options = webdriver.ChromeOptions()
    options.add_extension(proxyauth_plugin_path)
    # ${chromedriver_path}: chromedriver驱动存放路径
    driver = webdriver.Chrome(executable_path=r'C:\Users\Administrator\Desktop\chromedriver_win32\chromedriver.exe',
                              options=options)
    driver.get("https://dev.kdlapi.com/testproxy")

    # 获取页面内容
    print(driver.page_source)

    # 延迟3秒后关闭当前窗口,如果是最后一个窗口则退出
    time.sleep(8)
    driver.close()

运行效果:

Python调用ip代理案例,快代理api调用demo_Python教程_04

来源:https://www.kuaidaili.com/doc/dev/sdk_http/

Python调用ip代理案例,快代理api调用demo_Python教程_05      

微信公众号:二爷记

不定时分享python源码及工具