Python Selenium数据不可见;无误

2024-09-24 06:28:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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()

enter image description here

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


Tags: ofthesendidbyvaluedriverelement
1条回答
网友
1楼 · 发布于 2024-09-24 06:28:51

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

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)

相关问题 更多 >