【问题标题】:Access Denied You don't have permission to access site in Selenium Python [duplicate]访问被拒绝您无权访问 Selenium Python 中的站点 [重复]
【发布时间】:2022-01-18 19:57:18
【问题描述】:

这是我的 Python 代码, 在重新加载 webdriver 时运行此代码后,我遇到错误 Access Denied You don't have permission to access site

我也尝试过无头方法,但也不起作用。

from time import sleep
import random
from selenium.webdriver.common.keys import Keys
import selenium
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options



class StockData:
    def __init__(self):
#         chrome_options = Options()
#         chrome_options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36')
#         self.driver=webdriver.Chrome(executable_path=r'C:\Users\Arth\Desktop\codes\chromedriver.exe',chrome_options=chrome_options)
#         agent = self.driver.execute_script("return navigator.userAgent")
        options = Options()
        options.headless = True
        profile = webdriver.FirefoxProfile()
        profile.set_preference("general.useragent.override", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36")
        self.driver = webdriver.Firefox(profile,executable_path=r'C:\Users\Arth\Desktop\codes\geckodriver.exe')

        
    def goSite(self):
        driver=self.driver
        driver.get("https://www.nseindia.com/market-data/volume-gainers-spurts")
        sleep(3)
    def start(self):
        driver=self.driver
        #driver.find_element_by_xpath("//img[contains(@title, 'Refresh') ]").click()
        sleep(1)
        names=driver.find_elements_by_xpath("//a[contains(@title, '_blank') ]")
        print(names)
        
bot=StockData()
bot.goSite()
bot.start()```





【问题讨论】:

标签: javascript python selenium selenium-webdriver webdriver


【解决方案1】:

此错误消息...

Access Denied You don't have permission to access site

...暗示Selenium 驱动GeckoDriver 发起 浏览上下文detected as a BOT 并且进一步导航被阻止。


长期以来,NSE India 一直受到 Bot Manager 的保护,这是一种由 Akamai 提供的高级机器人检测服务,并且响应被阻止

您可以在以下位置找到一些相关的详细讨论:


解决方案

要隐藏WebDriver / GeckoDriver 是自动化驱动的事实,您可以按照讨论中提到的步骤How to Conceal WebDriver in Geckodriver from BotD in Java?

【讨论】:

  • 非常感谢兄弟提供的链接帮助我解决了这个问题
  • chrome_options.add_experimental_option('useAutomationExtension', False) 当我在代码中添加它的工作时,上面这行!
【解决方案2】:

您的工作不需要硒。只需使用requests 库即可。

我们首先向主站点发出请求,以获取第二个请求的 cookie。然后我们向 API 发出第二个请求。

request.Session() 让我们使用存储上一个请求的 cookie。

您可以在浏览器的网络选项卡中查看他们发出的请求。

我使用了他们的 API 网址,如下所示。

import requests

session = requests.Session()
session.headers["User-Agent"] ="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0"

session.get("https://www.nseindia.com/market-data/volume-gainers-spurts")

result = session.get("https://www.nseindia.com/api/live-analysis-volume-gainers")

print(result.json())

我已经测试过了,它应该可以工作。

【讨论】:

    猜你喜欢
    • 2023-01-06
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 2016-01-31
    • 2020-05-05
    • 1970-01-01
    • 2019-05-09
    相关资源
    最近更新 更多