【发布时间】:2021-10-03 08:23:10
【问题描述】:
我正在尝试从以下页面提取数据:
https://www.lifeinscouncil.org/industry%20information/ListOfFundNAVs
我必须选择公司名称并从日历中选择一个日期,然后单击获取数据按钮。 我正在尝试使用 Python 中的 Chrome 使用 Selenium Web 驱动程序来实现这一点,我被困住了如何将日期参数传递给页面。 从日历中选择日期后,页面似乎是回发的。 需要从日历中选择日期,否则网页不返回数据。 我也尝试过使用 requests Post 方法,但无法获取 NAV 数据。
我需要在每天(交易日)的基础上迭代 5 年。
PS:我不擅长理解 DOM 元素,但对 Python 和编码有基本的了解。我的职业是数据分析师。
提前致谢。 基兰耆那教
编辑:在下面添加当前代码:
from selenium import webdriver
url='https://www.lifeinscouncil.org/industry%20information/ListOfFundNAVs'
opt = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
opt.add_argument("--start-maximized")
# opt.add_argument("--headless")
opt.add_argument("--disable-notifications")
opt.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(options=opt)
driver.get('https://www.lifeinscouncil.org/industry%20information/ListOfFundNAVs');
insurer = driver.find_element_by_id("MainContent_drpselectinscompany")
nav_date=driver.find_element_by_id('MainContent_txtdateselect')
get_data_btn=driver.find_element_by_id('MainContent_btngetdetails')
options=insurer.find_elements_by_tag_name("option")
data=[]
row={'FirmName','SFIN','fundName','NAVDate','NAV'}
for option in options:
print('here')
print(option.get_attribute("value") + ' ' + option.text)
if(option.text!='--Select Insurer--'):
option.click()
driver.find_element_by_id("MainContent_imgbtncalender").click()#Calender Icon
driver.find_element_by_link_text("June").click()#Date
driver.find_element_by_link_text("25").click()#Date
get_data_btn=driver.find_element_by_id('MainContent_btngetdetails') #this is put here again because on clicking the date, the page is reloaded
get_data_btn.click()
print('clicked')
driver.quit()
【问题讨论】:
-
有没有试过的代码?
-
添加的代码,但这不起作用会在单击后循环回到第二个元素时出错,selenium.common.exceptions.StaleElementReferenceException 消息:过时的元素参考:所选日期只是一个示例 - 我会需要循环遍历从 2016 年 1 月 1 日到今天的所有日期 - 这些日期可以作为参数传递给将通过批处理文件生成的 exe(仅将交易日期作为参数传递)或构建更多错误检查/尝试捕获处理非交易日的类型
-
我现在正在尝试使用 POST 方法获取数据。但是我被卡住了,因为视图状态随着网站上选择的每个参数而不断变化。以下是单独询问的帖子。 stackoverflow.com/questions/69388379/…
标签: python selenium google-chrome webdriver