技术问题
关于 OkkProxy 技术集成和使用的常见问题。
支持哪些编程语言?
OkkProxy 代理可以与任何支持 HTTP/HTTPS 或 SOCKS5 协议的编程语言配合使用。
常用语言示例
Python
python
import requests
proxies = {
'http': 'http://username:[email protected]:8080',
'https': 'http://username:[email protected]:8080'
}
response = requests.get('https://example.com', proxies=proxies)
print(response.text)1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Node.js
javascript
const axios = require('axios');
const proxy = {
host: 'proxy.okkproxy.com',
port: 8080,
auth: {
username: 'your_username',
password: 'your_password'
}
};
axios.get('https://example.com', { proxy })
.then(response => console.log(response.data))
.catch(error => console.error(error));1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Java
java
import java.net.*;
import java.io.*;
public class ProxyExample {
public static void main(String[] args) throws Exception {
String proxyHost = "proxy.okkproxy.com";
int proxyPort = 8080;
String username = "your_username";
String password = "your_password";
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxyHost, proxyPort));
URL url = new URL("https://example.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
PHP
php
<?php
$proxy = 'proxy.okkproxy.com:8080';
$proxyAuth = 'username:password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com');
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
如何在浏览器中使用代理?
Chrome/Edge
- 安装代理扩展(如 Proxy SwitchyOmega)
- 配置代理服务器信息
- 启用代理
Firefox
- 打开设置 → 网络设置
- 选择"手动配置代理"
- 填写代理服务器和端口
- 勾选"为所有协议使用相同代理"
Safari
- 打开系统偏好设置 → 网络
- 选择当前网络连接
- 点击"高级" → "代理"
- 配置 HTTP 和 HTTPS 代理
如何设置爬虫框架使用代理?
Scrapy
python
# settings.py
DOWNLOADER_MIDDLEWARES = {
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware': 1,
}
# 在爬虫中使用
class MySpider(scrapy.Spider):
name = 'myspider'
def start_requests(self):
proxy = 'http://username:[email protected]:8080'
for url in self.start_urls:
yield scrapy.Request(url,
meta={'proxy': proxy})1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Selenium
python
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = 'proxy.okkproxy.com:8080'
proxy.ssl_proxy = 'proxy.okkproxy.com:8080'
capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get('https://example.com')1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
Playwright
python
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(proxy={
'server': 'http://proxy.okkproxy.com:8080',
'username': 'your_username',
'password': 'your_password'
})
page = browser.new_page()
page.goto('https://example.com')
browser.close()1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Puppeteer
javascript
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
args: [
'--proxy-server=proxy.okkproxy.com:8080'
]
});
const page = await browser.newPage();
await page.authenticate({
username: 'your_username',
password: 'your_password'
});
await page.goto('https://example.com');
await browser.close();
})();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
如何处理超时问题?
设置超时时间
Python requests
python
import requests
proxies = {
'http': 'http://username:[email protected]:8080'
}
try:
response = requests.get(
'https://example.com',
proxies=proxies,
timeout=30 # 30秒超时
)
except requests.Timeout:
print("请求超时")1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Node.js axios
javascript
axios.get('https://example.com', {
proxy: {
host: 'proxy.okkproxy.com',
port: 8080
},
timeout: 30000 // 30秒超时
})1
2
3
4
5
6
7
2
3
4
5
6
7
超时问题排查
- 检查网络连接
- 尝试更换代理节点
- 增加超时时间
- 查看目标网站响应速度
如何处理 SSL 证书错误?
Python
python
import requests
proxies = {
'http': 'http://username:[email protected]:8080',
'https': 'http://username:[email protected]:8080'
}
# 禁用 SSL 验证(不推荐在生产环境使用)
response = requests.get(
'https://example.com',
proxies=proxies,
verify=False
)
# 或者指定证书路径
response = requests.get(
'https://example.com',
proxies=proxies,
verify='/path/to/cert.pem'
)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Node.js
javascript
const https = require('https');
const axios = require('axios');
const agent = new https.Agent({
rejectUnauthorized: false // 禁用证书验证
});
axios.get('https://example.com', {
httpsAgent: agent,
proxy: {
host: 'proxy.okkproxy.com',
port: 8080
}
});1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
如何实现请求重试?
Python with retrying
python
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import requests
# 配置重试策略
retry_strategy = Retry(
total=3, # 最多重试3次
backoff_factor=1, # 重试间隔
status_forcelist=[429, 500, 502, 503, 504]
)
adapter = HTTPAdapter(max_retries=retry_strategy)
session = requests.Session()
session.mount("http://", adapter)
session.mount("https://", adapter)
proxies = {
'http': 'http://username:[email protected]:8080'
}
response = session.get('https://example.com', proxies=proxies)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Node.js with axios-retry
javascript
const axios = require('axios');
const axiosRetry = require('axios-retry');
axiosRetry(axios, {
retries: 3, // 重试次数
retryDelay: axiosRetry.exponentialDelay,
retryCondition: (error) => {
return axiosRetry.isNetworkOrIdempotentRequestError(error)
|| error.response.status === 429;
}
});
axios.get('https://example.com', {
proxy: {
host: 'proxy.okkproxy.com',
port: 8080
}
});1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
如何管理代理池?
简单代理池实现
python
import requests
import random
from queue import Queue
class ProxyPool:
def __init__(self):
self.proxies = Queue()
self.bad_proxies = set()
def add_proxy(self, proxy):
"""添加代理到池中"""
self.proxies.put(proxy)
def get_proxy(self):
"""获取一个可用代理"""
if self.proxies.empty():
raise Exception("代理池为空")
return self.proxies.get()
def return_proxy(self, proxy, is_good=True):
"""归还代理"""
if is_good:
self.proxies.put(proxy)
else:
self.bad_proxies.add(proxy)
def test_proxy(self, proxy):
"""测试代理是否可用"""
try:
response = requests.get(
'https://httpbin.org/ip',
proxies={'http': proxy, 'https': proxy},
timeout=10
)
return response.status_code == 200
except:
return False
# 使用示例
pool = ProxyPool()
# 添加代理
proxies_list = [
'http://username:[email protected]:8080',
'http://username:[email protected]:8080',
'http://username:[email protected]:8080',
]
for proxy in proxies_list:
if pool.test_proxy(proxy):
pool.add_proxy(proxy)
# 使用代理
proxy = pool.get_proxy()
try:
response = requests.get('https://example.com',
proxies={'http': proxy})
pool.return_proxy(proxy, is_good=True)
except:
pool.return_proxy(proxy, is_good=False)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
如何监控代理状态?
实时监控脚本
python
import requests
import time
from datetime import datetime
def monitor_proxy(proxy, interval=60):
"""
监控代理状态
proxy: 代理地址
interval: 检查间隔(秒)
"""
while True:
try:
start_time = time.time()
response = requests.get(
'https://httpbin.org/ip',
proxies={'http': proxy, 'https': proxy},
timeout=10
)
response_time = time.time() - start_time
status = {
'time': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'proxy': proxy,
'status': 'OK' if response.status_code == 200 else 'FAIL',
'response_time': f'{response_time:.2f}s',
'ip': response.json().get('origin', 'Unknown')
}
print(f"[{status['time']}] {status['proxy']} - "
f"{status['status']} - {status['response_time']} - "
f"IP: {status['ip']}")
except Exception as e:
print(f"[{datetime.now()}] {proxy} - ERROR: {str(e)}")
time.sleep(interval)
# 使用
proxy = 'http://username:[email protected]:8080'
monitor_proxy(proxy, interval=300) # 每5分钟检查一次1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
API 使用限制
请求频率限制
- 基础套餐:100 请求/分钟
- 专业套餐:500 请求/分钟
- 企业套餐:无限制
并发连接限制
- 基础套餐:10 个并发
- 专业套餐:50 个并发
- 企业套餐:200 个并发
超出限制的处理
- 返回 429 状态码
- 实施指数退避重试
- 考虑升级套餐
还有其他技术问题?
查看更多技术文档: