您知道如何使用 PrivateProxy.me 代理?这是 PrivateProxy.me 官方最全面的介绍。
不同语言的用法示例
警告 这些示例旨在演示代理服务器的工作原理。未经大量修改,请勿在生产中使用。
在 *nix shell 中使用 curlPython 3 + 请求PHP + libcurlNode.js + stdlibRuby + stdlibGolang+stdlibC# + stdlib
curl -x http://pvtyproxies:[email protected]:5432 http://api.privateproxy.me:10738 #2.57.20.194
导入请求 代理服务器 = { 'http':http://pvtyproxies:[email protected]:5432'、 https': 'http://pvtyproxies:[email protected]:5432'. } response = requests.get('http://api.privateproxy.me:10738', proxies=proxies) // response.text = "2.57.20.194\\n"
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://api.privateproxy.me:10738"); curl_setopt($ch, CURLOPT_PROXY, "2.57.20.194:5432"); curl_setopt($ch, CURLOPT_PROXYUSERPWD, "pvtyproxies:ajd89akjdAdk"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); $curl_scraped_page = curl_exec($ch); curl_close($ch); echo $curl_scraped_page; // "2.57.20.194\\n"
const http = require('http'); const https = require('https'); function getWithProxy(url, proxy) { const parsedUrl = new URL(url); const proxy_ip = proxy['ip'] const proxy_port = proxy['port'] const proxy_auth = 'Basic ' + Buffer.from(proxy['login'] + ':' + proxy['password']).toString('base64') let host = parsedUrl.hostname; if (parsedUrl.port !== '') { host += ':' + parsedUrl.port; } else { if (parsedUrl.protocol == 'http:') { host += ':80'; } else if(parsedUrl.protocol == 'https:') { host += ':443'; } } const res = new Promise((resolve, reject) => { http.request({ port: proxy_port, host: proxy_ip, method: 'CONNECT', path: host, headers: { 'Host': host, 'Proxy-Authorization': proxy_auth } }).on('connect', (res, socket, head) => { if (res.statusCode !== 200) { reject(new Error(`Non-200 status code returned from proxy: ${res.statusCode} ${res.statusMessage}`)); return; } let t, real_opts = {}; if (parsedUrl.protocol == "http:") { real_opts['createConnection'] = () => socket t = http; } else { real_opts['socket'] = socket; t = https; } const real_req = t.request(url, real_opts, (res) => { res.setEncoding('utf-8') let rawData = [] res.on('data', (chunk) => rawData.push(chunk)); res.on('end', () => resolve(rawData.join(''))); }); real_req.on('error', (e) => reject(e)); real_req.end(); }).end(); }); return res; } getWithProxy("https://httpbin.org/ip", { ip: '2.57.20.194', port: 5432, login: 'pvtyproxies', password: 'ajd89akjdAdk' }).then( (data) => console.log(data.trim()), (e) => console.log("Error: " + e.toString()) ).then( () => getWithProxy("http://api.privateproxy.me:10738", { ip: '2.57.20.194', port: 5432, login: 'pvtyproxies', password: 'ajd89akjdAdk' }) ).then( (data) => console.log(data.trim()), (e) => console.log("Error: " + e.toString()) )
需要 "net/http resp = Net::HTTP.start("api.privateproxy.me", 10738, "2.57.20.194", "5432", "pvtyproxies", "ajd89akjdAdk") do |h| request = Net::HTTP::Get.new('/') h.request(request) 结束 puts resp.body # "2.57.20.194\\n"
主包 导入 ( "fmt" "io/ioutil" "net/http" "net/url" "时间" ) func main() { proxy, err := url.Parse("http://pvtyproxies:[email protected]:5432") if err != nil { 返回 } proxyFunc := http.ProxyURL(proxy) tr := &http.Transport{ MaxIdleConns: 10, IdleConnTimeout: 30 * time.Second、 DisableCompression: true、 代理:proxyFunc、 } 客户端 := &http.Client{ Transport: tr、 } resp, err := client.Get("http://api.privateproxy.me:10738") if err != nil { 返回 } defer resp.Body.Close() bytes, err := ioutil.ReadAll(resp.Body) if err != nil { 返回 } fmt.Print(string(bytes))// "2.57.20.194\\n" }
var handler = new HttpClientHandler(); handler.Proxy = new WebProxy("http://pvtyproxies:[email protected]:5432/"); using (var httpClient = new HttpClient(handler)) { using (var request = new HttpRequestMessage(new HttpMethod("GET"), "http://api.privateproxy.me:10738")) { var response = await httpClient.SendAsync(request); // response.Content - "2.57.20.194\\n" } }
http://api.privateproxy.me:10738 - 以文本模式显示您当前的 IP 地址
使用反向连接代理与使用普通代理没有区别。Backconnect 代理服务器的作用与普通代理服务器相同,只是目标网站看到的 IP 地址不同,具体取决于交换间隔设置/手动交换。
如果您已将自己的 IP 地址添加到授权 IP 列表中,则无需指定证书。授权 IP 并不是强制性的,您可以继续使用证书授权,哪种方式更方便都可以。
如果您要编写自己的软件来访问某个网站,请添加 User-Agent 等标头,以便请求看起来像是来自浏览器。
浏览器使用建议
- 关闭 WebRTC,因为即使使用代理服务器,它也会破坏你的真实 IP 地址,甚至你的本地内网 IP 地址。这里有一个针对不同浏览器的禁用指南链接: https://www.vpnunlimitedapp.com/blog/how-to-disable-webrtc-in-chrome-and-other-browsers
- 使用 Chrome 浏览器的 ghostery 等禁用跟踪器插件,网站仍能通过浏览器指纹识别你的身份。
- 如果可能,将本地计算机时间设置为代理服务器时区。
- 如果使用 backconnect,最好将交换时间间隔设置为 5 分钟或更长,这样就不会经常出现 IP 变更。
使用 Python-Selenium 代理
python-selenium + Firefox(geckodriver)
要在 firefox 中使用 selenium,你需要安装这些项目:
- https://pypi.org/project/selenium/
- geckodriver: https://github.com/mozilla/geckodriver/releases
- 已安装的 Firefox 实例
如果使用凭据认证,还需要
例如,使用代理凭据:
from seleniumwire import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium.webdriver.firefox.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC path_to_firefox_binary = '' # !!! 设置此变量 !!! path_too_geckodriver_binary = '' # !!! 设置此变量 !!! wire_options = { 代理':{ 'http':http://pvtyproxies:[email protected]:5432'、 https': 'http://pvtyproxies:[email protected]:5432'、 no_proxy':localhost,127.0.0.1'。 } } binary = FirefoxBinary(path_to_firefox_binary) options = Options() options.headless = True browser = webdriver.Firefox( options=options、 firefox_binary=binary、 executable_path=path_to_geckodriver_binary、 seleniumwire_options=wire_options ) browser.get("http://api.privateproxy.me:10738") element = WebDriverWait(browser, 10).until( EC.visibility_of_element_located((By.CSS_SELECTOR, "pre")) ) print(element.text) browser.quit()
使用 IP 授权的示例:
from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium.webdriver.firefox.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC path_to_firefox_binary = '' # !!! 设置此变量 !!! path_too_geckodriver_binary = '' # !!! 设置此变量 !!! proxy_ip = '2.57.20.194 代理端口 = 5432 profile = webdriver.FirefoxProfile() profile.set_preference("network.proxy.type", 1) profile.set_preference("network.proxy.http", proxy_ip) profile.set_preference("network.proxy.http_port", proxy_port) profile.set_preference("network.proxy.ssl", proxy_ip) profile.set_preference("network.proxy.ssl_port", proxy_port) profile.update_preferences() binary = FirefoxBinary(path_to_firefox_binary) options = Options() options.headless = True 浏览器 = webdriver.Firefox( options=options、 firefox_binary=binary、 executable_path=path_to_geckodriver_binary、 firefox_profile=profile ) browser.get("http://api.privateproxy.me:10738") element = WebDriverWait(browser, 10).until( EC.visibility_of_element_located((By.CSS_SELECTOR, "pre")) ) print(element.text) browser.quit()
这两个示例都以无头模式启动 firefox,因此在执行这些示例时浏览器将保持隐藏状态。
python-selenium + Chrome(chromedriver)
要在 Chrome 浏览器中使用硒,你需要安装这些项目:
- https://pypi.org/project/selenium/
- cromedriver: https://chromedriver.chromium.org/downloads
- 已安装的 Chrome 浏览器实例
使用凭证授权的示例:
导入 os 导入 zipfile 导入 hashlib from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC 代理="2.57.20.194:5432"。 凭证 = "pvtyproxies:ajd89akjdAdk" path_to_chrome_binary = '' !!! 设置此变量 !.! path_too_chromedriver_binary = '' !!! 设置此变量 !!! def generate_extension(proxy, credentials): IP、端口 = proxy.split(':') login, password = credentials.split(':') manifest_json = """ { 版本"1.0.0", "manifest_version":2, "名称":"Chrome 代理"、 "权限": [[ "代理"、 "选项卡"、 "unlimitedStorage"、 "存储"、 "", "网络请求"、 "网络请求阻塞" ], "背景":{ "脚本":["background.js"] }, "minimum_chrome_version":"22.0.0" } """ background_js = """ var config = { mode:"fixed_servers"、 rules:{ singleProxy:{ scheme:"http"、 host: "%s"、 port: parseInt(%s) }, bypassList:["localhost"](本地主机 } }; chrome.proxy.settings.set({value: config, scope:"regular"}, function() {}); function callbackFn(details) { 返回 { authCredentials:{ username: "%s"、 password: "%s". } }; } chrome.webRequest.onAuthRequired.addListener( callbackFn、 {urls:[""]}, ['阻塞'] ); """% (IP、端口、登录名、密码) sha1 = hashlib.sha1() sha1.update(("%s:%s" % (proxy, credentials)).encode('utf-8')) 文件名 = sha1.hexdigest() + ".zip" with zipfile.ZipFile(filename, 'w') as zp: zp.writestr("manifest.json", manifest_json) zp.writestr("background.js", background_js) 返回文件名 extension_name = generate_extension(proxy, credentials) options = Options() options.binary_location = path_to_chrome_binary options.add_extension(extension_name) 驱动程序 = webdriver.Chrome( executable_path = path_to_chromedriver_binary、 options=options、 ) driver.get('http://api.privateproxy.me:10738/') element = WebDriverWait(driver, 30).until( EC.visibility_of_element_located((By.CSS_SELECTOR, "pre")) ) print(element.text) driver.quit() os.remove(extension_name)
遗憾的是,在使用凭证时存在一些限制:
- 没有无头模式
- 必须为每个代理 + 凭据集生成并存储扩展名。我们找不到更简单的方法。
使用 ip 授权的示例:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC path_too_chrome_binary = '' # !!! 设置此变量 !!! path_too_chromedriver_binary = '' # !!! 设置此变量 !!! 代理 = "2.57.20.194:5432" options = Options() options.binary_location = path_to_chrome_binary options.headless = True options.add_argument("--proxy-server=%s" % proxy) 驱动程序 = webdriver.Chrome( executable_path = path_to_chromedriver_binary、 options=options、 ) driver.get('http://api.privateproxy.me:10738/') element = WebDriverWait(driver, 30).until( EC.visibility_of_element_located((By.CSS_SELECTOR, "pre")) ) print(element.text) driver.quit()
使用 Node.JS Puppeteer 代理
Puppeteer + Node.js + chrome 浏览器
目前,我们只有一个如何在 puppeteer 和 Chrome 浏览器中使用代理的示例。
为此,您需要在安装 puppeteer 时使用环境变量 PUPPETEER_PRODUCT=chrome:
PUPPETEER_PRODUCT=chrome npm install puppeteer
您还需要代理链软件包:
npm install proxy-chain
代码示例
const puppeteer = require('puppeteer'); const proxyChain = require('proxy-chain'); (async() => { const oldProxyUrl = 'http://pvtyproxies:[email protected]:5432'; const newProxyUrl = await proxyChain.anonymizeProxy(oldProxyUrl); const browser = await puppeteer.launch({ args: [`--proxy-server=${newProxyUrl}`]、 }); const page = await browser.newPage(); await page.goto('https://httpbin.org/ip'); const element = await page.$('pre'); const text = await page.evaluate(element => element.textContent, element); console.log(text); await browser.close(); await proxyChain.closeAnonymizedProxy(newProxyUrl, true); })();
最后更新于 2023 年 12 月 12 日