Я пытаюсь получить базу данных с веб-сайта, страница показывает эту информацию по месяцам. В частности, в раскрывающемся меню, где можно выбрать месяц и год, и все данные под этим полем обновляются с выбранной опцией.
Я использую Selenium для выбора опции, но проблема в том, что она появляется вв соответствующем поле выбрана опция. Однако данные не обновляются. Требуется ли какой-либо щелчок или другая функция для достижения этой обновленной даты после выбора опции?
Я использую Python, Selenium и веб-страницу, которая находится в коде. Выбран вариант, но он не работает.
Я использую python, selenium и все библиотеки, которые показаны в первой части кода
Код здесь:
import numpy as np
import io
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
from sklearn import datasets, linear_model
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
a=[]
b=[]
url="https://www.integra.com.pe/wps/portal/integra/personas/inversiones-y-rentabilidad/valor-cuota/consulta-el-valor-cuota"
# Start webdriver with Chrome
browser = webdriver.Chrome(executable_path=r"D:\python\afp\driver\chromedriver.exe")
browser.get(url)
lista_menu = browser.find_element_by_class_name('select')
drp=Select(lista_menu)
#This loop is for obtain all the options for the dropmenu
for option in drp.options:
a=option.get_attribute("value")
b.append(a)
print(b)
browser.close()
#This is for begin another web browser, i am doing this because I obtain errors otherwise
browser = webdriver.Chrome(executable_path=r"D:\python\afp\driver\chromedriver.exe")
browser.get(url)
lista_menu = browser.find_element_by_class_name('select')
drp=Select(lista_menu)
#browser.find_element_by_class_name('icon-inner').click()
lista_menu = browser.find_element_by_class_name('select')
drp=Select(lista_menu)
drp.select_by_value("201905")
#This loop is to obtain data considering all the values obtained in the first loop, however the problem is that the field change
# the value but the data, all the rows below mantain the same information
for month in b:
browser = webdriver.Chrome(executable_path=r"D:\python\afp\driver\chromedriver.exe")
browser.get(url)
lista_menu = browser.find_element_by_class_name('select')
drp=Select(lista_menu)
drp.select_by_value(month)
element = browser.find_element_by_class_name('table-4').text
print(element)
df = pd.read_fwf(io.StringIO(element),head=None, widths=[11, 12, 12, 12, 12], names=['Fecha', 'Fondo0', 'Fondo1', 'Fondo2', 'Fondo3'])
df = df.drop([0], axis=0)
df['Fecha'] = pd.to_datetime(df['Fecha'], format="%d/%m/%Y")
print(df)
df['Fecha'] = df['Fecha'].map(dt.datetime.toordinal)
Fecha=df.iloc[:,0].values.reshape(-1,1)
print(Fecha)
Fondo3=df.iloc[:,4].values.reshape(-1,1)
print(Fondo3)
regr = linear_model.LinearRegression()
regr.fit(Fecha, Fondo3)
y_pred = regr.predict(Fecha)
print(regr.coef_)
print(regr.intercept_)
plt.scatter(Fecha, Fondo3)
plt.plot(Fecha, y_pred, color='red')
plt.show()