Python Selenium Chrome驱动程序-nseindia-获取403



在此处输入图像描述

"试图从NSE获取股票衍生品数据https://www.nseindia.com/->市场数据->衍生品市场在点击操作之前一直有效,浏览器会导航到衍生品市场,但随后会中断访问拒绝错误如下

<h1>
Access Denied
</h1>
You don't have permission to access "http://www.nseindia.com/market-data/equity-derivatives-watch" on this server.
<p>
Reference #18.386dcc17.1603823463.54b06d7
</p>
'''
from selenium import webdriver
from selenium.webdriver import ActionChains
from bs4 import BeautifulSoup
import time
# Tried all possible options below
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.nseindia.com/")
marketdata = driver.find_element_by_xpath("//*[@id='main_navbar']/ul/li[3]/a")
derivativesmarket = driver.find_element_by_xpath("//*[@id='main_navbar']/ul/li[3]/div/div[1]/div/div[1]/ul/li[3]/a")
actions = ActionChains(driver)
actions.move_to_element(marketdata).move_to_element(derivativesmarket).click().perform()
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
# soup = BeautifulSoup(html,'lxml')
time.sleep(7)
print(soup.prettify())[enter image description here][1]

添加

options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

并使用

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

然后访问这样的元素:

WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h1.h1")))

参考:-访问被拒绝您没有';t具有访问";站点";在该服务器上使用ChromeDriver和Chrome通过Selenium Python

相关内容

  • 没有找到相关文章

最新更新