You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何验证Selenium(Python)中代理服务器是否已替换真实IP?

Verify if Your Proxy is Working in Selenium (Python)

Hey there! Let's break down how to confirm that your proxy is successfully masking your real IP address in your Selenium script. Here are a couple of easy, reliable methods:

Method 1: Fetch Your Public IP Directly in the Script

The simplest way is to have your browser visit an IP-checking service and grab the displayed IP. You can integrate this right into your existing code to automate the verification.

Modify your script like this:

# selenium imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import random

PROXY ="88.157.149.250:8080";
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)

LINKS_XPATH = '//*[contains(@id,"result")]/div/div[3]/div[1]/a'
browser = webdriver.Chrome(chrome_options=chrome_options)

# First, check your current IP
browser.get("https://api.ipify.org")
# Wait for the page to load and get the IP text
current_ip = WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.TAG_NAME, "body"))
).text
print(f"Proxy IP in use: {current_ip}")
# Compare this to your actual public IP (check manually via a different browser if needed)

# Now proceed with your Amazon scraping
browser.get( 'https://www.amazon.com/s/ref=lp_11444071011_nr_p_8_1/132-3636705-4291947?rh=n%3A3375251%2Cn%3A%213375301%2Cn%3A10971181011%2Cn%3A11444071011%2Cp_8%3A2229059011')
links = browser.find_elements_by_xpath(LINKS_XPATH)
for link in links:
    href = link.get_attribute('href')
    print(href)

browser.quit()

When you run this, if the printed IP matches your proxy 88.157.149.250, your proxy is working correctly. If it shows your real IP, double-check:

  • Is the proxy server still active and reachable?
  • Are there any system-level proxy settings overriding your Chrome options?
  • Did you format the --proxy-server argument correctly (it should follow the ip:port format)?

Method 2: Inspect Network Requests Manually

If you prefer a visual check:

  1. Add chrome_options.add_argument("--auto-open-devtools-for-tabs") to your Chrome options before launching the browser.
  2. When the browser opens, switch to the Network tab in DevTools.
  3. Load the Amazon page (or an IP-check site), click on any request in the list, and look for the Remote Address field in the request details. It should show your proxy IP, not your real one.

Either method will confirm whether your proxy is successfully replacing your real IP. Happy scraping!

内容的提问来源于stack exchange,提问作者ryy77

火山引擎 最新活动