Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium-数据不可见;无误_Python_Python 3.x_Selenium_Web Scraping_Selenium Chromedriver - Fatal编程技术网

Python Selenium-数据不可见;无误

Python Selenium-数据不可见;无误,python,python-3.x,selenium,web-scraping,selenium-chromedriver,Python,Python 3.x,Selenium,Web Scraping,Selenium Chromedriver,我正在尝试使用Selenium从网站上抓取数据,我能够发送值,但没有收到开始抓取的结果。程序也没有抛出任何错误。以下是可复制代码: # Setting up driver driver = webdriver.Chrome(executable_path='D:\Program Files\ChromeDriver\chromedriver.exe') # Opening up the webpage driver.get("https://www1.nseindia.com/pro

我正在尝试使用Selenium从网站上抓取数据,我能够发送值,但没有收到开始抓取的结果。程序也没有抛出任何错误。以下是可复制代码:

# Setting up driver
driver = webdriver.Chrome(executable_path='D:\Program Files\ChromeDriver\chromedriver.exe')

# Opening up the webpage
driver.get("https://www1.nseindia.com/products/content/derivatives/equities/historical_fo.htm")

# Setting the value of Select Instrument
driver.find_element_by_id('instrumentType').send_keys('Index Options')

# Setting the value of Select Symbol
driver.find_element_by_id('symbol').send_keys('NIFTY 50')

# Setting the value of Select Year
driver.find_element_by_id('year').send_keys('2019')

# Setting the value of Select Expiry
select = Select(driver.find_element_by_id('expiryDate'))
noOfExpiries = 2
select.select_by_index(noOfExpiries)

# Setting the value of Select Option Type
cycle = 'PE'
driver.find_element_by_id('optionType').send_keys(cycle)

# Clicking the date range radio button
driver.find_element_by_xpath("//input[@id='rdDateToDate']").click()

# Setting the date range
fromDate = (datetime.strptime(select.options[noOfExpiries].text, '%d-%m-%Y') - timedelta(days=45)).strftime("%d-%b-%Y")
driver.find_element_by_id('fromDate').send_keys(fromDate)

toDate = datetime.strptime(select.options[noOfExpiries].text, '%d-%m-%Y').strftime("%d-%b-%Y")
driver.find_element_by_id('toDate').send_keys(toDate)

print(fromDate, toDate)

# Clicking the Get Data button
driver.find_element_by_id('getButton').click()


关于我在这里遗漏了什么,有什么线索吗?

好的,这就够了。我提出基于硒的解决方案只是因为您对请求模块表示不感兴趣:

from selenium import webdriver
from datetime import datetime, timedelta
from selenium.webdriver.common.by import By
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = ChromeOptions()
options.add_argument('disable-blink-features=AutomationControlled')

with webdriver.Chrome(options=options) as driver:
    wait = WebDriverWait(driver,10)

    driver.get("https://www1.nseindia.com/products/content/derivatives/equities/historical_fo.htm")
    wait.until(EC.presence_of_element_located((By.ID, "instrumentType"))).send_keys('Index Options')
    wait.until(EC.presence_of_element_located((By.ID, "symbol"))).send_keys('NIFTY 50')
    wait.until(EC.presence_of_element_located((By.ID, "year"))).send_keys('2019')
    select = Select(wait.until(EC.presence_of_element_located((By.ID, "expiryDate"))))
    noOfExpiries = 13
    select.select_by_index(noOfExpiries)
    cycle = 'PE'

    wait.until(EC.presence_of_element_located((By.ID, "optionType"))).send_keys(cycle)
    item = wait.until(EC.presence_of_element_located((By.XPATH, "//input[@id='rdDateToDate']")))
    driver.execute_script("arguments[0].click();",item)
    fromDate = (datetime.strptime(select.options[noOfExpiries].text, '%d-%m-%Y') - timedelta(days=45)).strftime("%d-%b-%Y")
    wait.until(EC.presence_of_element_located((By.ID, "fromDate"))).send_keys(fromDate)

    toDate = datetime.strptime(select.options[noOfExpiries].text, '%d-%m-%Y').strftime("%d-%b-%Y")
    wait.until(EC.presence_of_element_located((By.ID, "toDate"))).send_keys(toDate,Keys.ENTER)

    print(fromDate, toDate)
    search_button = wait.until(EC.presence_of_element_located((By.ID, "getButton")))
    driver.execute_script("arguments[0].click();",search_button)
    tabular_content = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#historicalData > table"))).get_attribute("innerHTML")
    print(tabular_content)

点击后,你不会从网站上刮取任何内容。点击“获取数据”后,我没有收到要从中刮取数据的元素。我看到代码中没有延迟。您是否定义了隐式等待超时?是的,延迟不是问题。您可以使用请求模块从该站点获取内容。你试过了吗?谢谢你的修复,甚至提高了代码质量!