繁体   English   中英

拒绝访问 您无权访问 Selenium Python 中的站点

[英]Access Denied You don't have permission to access site in Selenium Python

所以这是我的 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()```





此错误消息...

Access Denied You don't have permission to access site

...意味着Selenium驱动的GeckoDriver启动浏览上下文检测为 BOT ,进一步导航被阻止。


从历史上看, NSE India长期以来一直受到Akamai提供的高级机器人检测服务Bot Manager的保护,并且响应被阻止

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


解决方案

要隐藏WebDriver / GeckoDriver是自动化驱动的事实,您可以按照 Java 中的如何从 BotD 中隐藏 Geckodriver 中的 WebDriver 中提到的步骤进行操作?

您正在做的事情不需要 selenium。 只需使用requests库。

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

request.Session()允许使用存储上一个请求中的 cookies。

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

我在下面使用了他们的 API Url。

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())

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

暂无
暂无

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

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