python-3.x - 硒:绕过访问被拒绝
问题描述
我正在尝试使用 Selenium 浏览网站,但出现错误:拒绝访问。您无权访问此服务器上的“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)
如何解决?感谢您的帮助
解决方案
试试下面的代码。它对我有用-
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)
推荐阅读
- ruby-on-rails - How to include the new Stripe Checkout in Rails?
- google-apps-script - Google App Script continuefolderiteration not continuing from last folder, but restarting ever time
- ios - 删除然后将 itme 添加到 UITableView 时出现 NXException
- perl - Perl 循环仅一次
- mysql - ubuntu服务器上的mysql数据库备份(cronjob)问题
- php - Laravel (ver 6) php artisan route:list 在生产环境中返回“目标类不存在”错误(在本地环境中工作正常)
- javascript - div 布局阻止从垂直滚动到水平滚动的转换
- javascript - 如何使用电子中的按钮打开 exe 文件
- python - pandas wide_to_long IndexError 是什么意思?
- enums - Rust - 将特征包装在枚举中以进行内存布局和更简单的泛型?