将Shifter与以下工具配合使用 Python
几分钟内将 Shifter 的住宅和 ISP 代理集成到您的 Python 脚本中。与 requests、aiohttp、Scrapy、Selenium 及所有主流 Python HTTP 库无缝兼容。
快速入门
安装
pip install requests 基本用法
import requests
proxy_url = "customer-USERNAME-country-us-sid-123ABC:PASSWORD@p.shifter.io:443"
proxies = {
"http": proxy_url,
"https": proxy_url,
}
response = requests.get("https://ipinfo.io/json", proxies=proxies)
print(response.json())
# {"ip": "154.16.xxx.xxx", "city": "New York", "country": "US", ...} 功能特性
示例
基础 Requests 用法
将 Shifter 代理与 Python 最流行的 HTTP 库配合使用的最简方式。直接在用户名中设置国家、地区、城市或 ASN,并传入 `sid` 以在多次请求中保持相同 IP。
import requests
PROXY_USER = "customer-USERNAME-country-us-sid-123ABC"
PROXY_PASS = "PASSWORD"
PROXY_HOST = "p.shifter.io"
PROXY_PORT = "443"
proxies = {
"http": f"http://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}",
"https": f"http://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}",
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}
response = requests.get(
"https://example.com",
proxies=proxies,
headers=headers,
timeout=30,
)
print(f"Status: {response.status_code}")
print(f"Content length: {len(response.text)}") aiohttp 异步与粘性会话
使用 aiohttp 进行高性能异步爬取。在用户名中添加 `sid-XXX` 以在多次请求中保持相同 IP,或使用 `ttl-N` 设置 N 秒的定时粘性会话。
import aiohttp
import asyncio
import uuid
# sid pins the same IP for every request that shares this session id;
# ttl-300 keeps that IP for up to 300 seconds, then rotates.
session_id = uuid.uuid4().hex[:8]
PROXY_URL = (
f"customer-USERNAME-country-us-sid-{session_id}-ttl-300:"
f"PASSWORD@p.shifter.io:443"
)
async def fetch(session, url):
async with session.get(
url, proxy=PROXY_URL, timeout=aiohttp.ClientTimeout(total=30)
) as response:
return await response.text()
async def main():
urls = [
"https://example.com/login",
"https://example.com/dashboard",
"https://example.com/orders",
]
async with aiohttp.ClientSession() as session:
tasks = [fetch(session, url) for url in urls]
results = await asyncio.gather(*tasks, return_exceptions=True)
for url, result in zip(urls, results):
if isinstance(result, Exception):
print(f"Error fetching {url}: {result}")
else:
print(f"Fetched {url}: {len(result)} bytes")
asyncio.run(main()) Scrapy 中间件
通过自定义下载中间件将 Shifter 代理集成到您的 Scrapy 爬虫中。在用户名中添加 `country-uk`、`region-bavaria`、`city-london` 或 `asn-7922` 等选择器进行地理定向,并使用 `sid` 固定会话。
# middlewares.py
class ShifterProxyMiddleware:
PROXY_USER = "customer-USERNAME-country-uk-sid-456DEF"
PROXY_PASS = "PASSWORD"
PROXY_HOST = "p.shifter.io"
PROXY_PORT = "443"
def process_request(self, request, spider):
request.meta["proxy"] = (
f"http://{self.PROXY_USER}:{self.PROXY_PASS}"
f"@{self.PROXY_HOST}:{self.PROXY_PORT}"
)
# settings.py
DOWNLOADER_MIDDLEWARES = {
"myproject.middlewares.ShifterProxyMiddleware": 350,
}
# spider.py
import scrapy
class ProductSpider(scrapy.Spider):
name = "products"
start_urls = ["https://example.co.uk/products"]
def parse(self, response):
for product in response.css(".product-card"):
yield {
"title": product.css("h2::text").get(),
"price": product.css(".price::text").get(),
"url": product.css("a::attr(href)").get(),
}
next_page = response.css("a.next-page::attr(href)").get()
if next_page:
yield response.follow(next_page, self.parse) Selenium WebDriver
通过 Shifter 代理路由 Selenium 浏览器自动化。非常适合爬取 JavaScript 渲染的页面。将城市级定向与 `sid` 结合使用,可在整个浏览器会话中保持相同 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
PROXY_HOST = "p.shifter.io"
PROXY_PORT = "443"
PROXY_USER = "customer-USERNAME-country-us-city-newyork-sid-789GHI"
PROXY_PASS = "PASSWORD"
chrome_options = Options()
chrome_options.add_argument(
f"--proxy-server=http://{PROXY_HOST}:{PROXY_PORT}"
)
driver = webdriver.Chrome(options=chrome_options)
# Handle proxy authentication via browser extension or seleniumwire if needed
driver.get("https://example.com")
# Wait for dynamic content to load
wait = WebDriverWait(driver, 10)
element = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".content"))
)
print(f"Page title: {driver.title}")
print(f"Content: {element.text[:200]}")
driver.quit() 常见问题
关于将 Shifter 与 Python 搭配使用的常见问题。
将 proxies 字典传递给任意 requests 方法,并使用您的 Shifter 代理 URL。格式为:proxies = {"http": "customer-USERNAME-country-us-sid-123ABC:PASSWORD@p.shifter.io:443", "https": "customer-USERNAME-country-us-sid-123ABC:PASSWORD@p.shifter.io:443"}。然后调用 requests.get(url, proxies=proxies)。
开始将Shifter与以下工具配合使用 Python
在 5 分钟内将 Shifter 的 205M+ 住宅代理集成到您的 Python 脚本中。灵活的 IP 轮换、地理定位,以及与所有主流 Python 库的完整兼容性。