简体   繁体   English

selenium:绕过访问被拒绝

[英]selenium: bypass access denied

I'm trying to navigate a website with Selenium, but I'm getting an error: Access Denied.我正在尝试使用 Selenium 浏览网站,但出现错误:拒绝访问。 You do not have permission to access "http://tokopedia.com/" on this server.您无权访问此服务器上的“http://tokopedia.com/”。

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys




CHROMEDRIVER_PATH = r'C:/chromedriver.exe'
tokopedia = "https://tokopedia.com/"

options = Options()
options.add_argument("--headless")
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)



driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, chrome_options=options)
driver.get(tokopedia)

print(driver.page_source)

how to solve it?如何解决? Thank you for the help感谢您的帮助

Try the below code.试试下面的代码。 It is working for me -它对我有用-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

tokopedia = "https://tokopedia.com/"

options = Options()
options.add_argument("--headless")
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36'
options.add_argument('user-agent={0}'.format(user_agent))

driver = webdriver.Chrome(options=options)
driver.get(tokopedia)

print(driver.page_source)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM