GBProxies

Cách Sử Dụng Proxy với Selenium

Chrome trong Selenium không chấp nhận username và password trong cờ --proxy-server. Cách đơn giản nhất là selenium-wire, nó xử lý đăng nhập cho bạn.

Các bước

  1. Cài đặt selenium-wire bằng pip install selenium-wire
  2. Truyền URL proxy với username và password trong seleniumwire_options
  3. Khởi động driver và mở trang đích
  4. Dùng session id để giữ một IP cho toàn bộ bài test

Mã ví dụ

from seleniumwire import webdriver

proxy = "http://USERNAME-cc-ca:PASSWORD@gw.gbproxies.cc:10000"
driver = webdriver.Chrome(seleniumwire_options={
    "proxy": {"http": proxy, "https": proxy, "no_proxy": "localhost,127.0.0.1"}
})
driver.get("https://api.ipify.org")
print(driver.find_element("tag name", "body").text)
driver.quit()

Thay thế USERNAME và PASSWORD bằng thông tin đăng nhập proxy từ trang Proxies của bảng điều khiển. Cổng 10000 là HTTP residential; xem các cổng của mỗi sản phẩm bên dưới.

Mẹo

  • Tắt hình ảnh bằng tùy chọn Chrome để tiết kiệm GB
  • Chạy một driver cho mỗi phiên cố định

Câu hỏi thường gặp

Tôi có thể dùng Selenium thường mà không cần gói bổ sung không?

Chrome cần tiện ích mở rộng hoặc selenium-wire để đăng nhập proxy; Firefox có thể đọc thông tin từ profile.

Cách Sử Dụng Proxy với Selenium