首页 > 解决方案 > 在 linux 中使用 selenium headless 拒绝访问

问题描述

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains


options = Options()
ua = UserAgent()
userAgent = ua.random
print(userAgent)

# below trick saved my life
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument(f'user-agent={userAgent}')

# Optional argument, if not specified will search path.
driver = webdriver.Chrome('chromedriver',options=options)
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)
driver.execute_script("return navigator.userAgent")

# Scraping steps
url = "https://www.costco.com"
driver.get(url)
driver.delete_all_cookies()
time.sleep(3)
print(driver.title)
driver.quit()

试图访问这个 url 并且访问被拒绝
我认为这个网站阻止了我的 ip

操作系统:AWS Linux
Python:3.7

我已经尝试了以下方法

  1. 用户代理
  2. 等待
  3. 动作链
  4. delete_all_cookies()

请问有什么方法可以帮助我吗?

标签: pythonseleniumselenium-webdriver

解决方案


推荐阅读