Python Selenium Chrome驱动程序-nseindia-获取403
''试图从NSE获取股票衍生品数据 数据->衍生品市场 在单击操作之前,浏览器将导航到衍生品市场,但随后thorws访问 拒绝错误如下Python Selenium Chrome驱动程序-nseindia-获取403,python,selenium,selenium-chromedriver,http-status-code-403,Python,Selenium,Selenium Chromedriver,Http Status Code 403,''试图从NSE获取股票衍生品数据 数据->衍生品市场 在单击操作之前,浏览器将导航到衍生品市场,但随后thorws访问 拒绝错误如下 <h1> Access Denied </h1> You don't have permission to access "http://www.nseindia.com/market-data/equity-derivatives-watch" on this server. <p>
<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]
拒绝访问
您没有访问“”的权限http://www.nseindia.com/market-data/equity-derivatives-watch“在这台服务器上。
参考#18.386dcc17.1603823463.54b06d7
'''
从selenium导入webdriver
从selenium.webdriver导入操作链
从bs4导入BeautifulSoup
导入时间
#尝试了下面所有可能的选项
options=webdriver.ChromeOptions()
选项。添加参数(“开始最大化”)
选项。添加参数(“禁用信息栏”)
options.add_参数(“--disable extensions”)
options.add_参数('--disable gpu')
driver=webdriver.Chrome(Chrome\u options=options)
驱动程序。获取(“https://www.nseindia.com/")
marketdata=driver.find_element_by_xpath(“/*[@id='main\u navbar']]/ul/li[3]/a”)
派生市场=驱动程序。通过xpath(“//*[@id='main\u navbar']/ul/li[3]/div/div[1]/div/div[1]/ul/li[3]/a”)查找元素
动作=动作链(驱动程序)
操作。将\移动到\元素(marketdata)。将\移动到\元素(derivativesmarket)。单击()。执行()
html=driver.page\u源
soup=BeautifulSoup(html,'html.parser')
#soup=BeautifulSoup(html,'lxml')
时间。睡眠(7)
打印(soup.prettify())[在此处输入图像描述][1]
添加
和使用
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
然后访问元素,如下所示:
WebDriverWait(driver,20)。直到(位于的元素的可见性((By.CSS\u选择器,“h1.h1”))
参考资料:-谢谢您的回复,艾布拉尔·艾哈迈德。它在WebDriverWait方法中超时。获取错误如下,第34行,在WebDriverWait(driver,20)中。直到(EC.visibility_of_element_located((By.CSS_SELECTOR,“h1.h1”))文件“/Users/sandeSandeSangle/PycharmProjects/nse/venv/lib/python3.8/site packages/selenium/webdriver/support/wait.py”,第80行,直到引发TimeoutException(消息、屏幕、堆栈跟踪)selenium.common.exceptions.TimeoutException:Message:我希望您已经将
(By.CSS\u选择器,“h1.h1”)
更改为您要查找的元素?CSS\u选择器,“h1.h1”)
来自另一个答案。页面上没有这样的元素,因此出现错误。您需要将其替换为正在查找的元素的选择器。更改代码如下,driver=webdriver.Chrome(Chrome\u options=options)driver.get(“)marketdata=driver.find_element_by_xpath(“/*[@id='main\u navbar']/ul/li[3]/a”)派生市场=驱动程序。find_element_by_by_xpath(“/*[@id='main\u navbar']/ul/li[3]/div/div/div[1]/ul/li[3]/a“)actions=ActionChains(driver)actions。移动到元素(marketdata)。移动到元素(derivativemarket)。单击().perform()WebDriverWait(driver,20)。直到(EC.visibility_of_元素位于((By.ID,“tableLiveMarket股票衍生品”))我建议你再看一眼你正在抓取的网页的html
。很可能没有找到元素。我现在要睡觉了。祝你好运!服务器本身是否拒绝了你的访问?
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC