我之前有一些编码经验,但不是专门针对 Web 应用程序的。我的任务是从这个网站获取数据:http : //www.b3.com.br/pt_br/market-data-e-indices/servicos-de-dados/market-data/consultas/mercado-de-derivativos /precos-referenciais/taxas-referenciais-bm-fbovespa/
它们每天都可用。我在 Python 中使用了 selenium,到目前为止结果很好:我可以获取整个表,将其存储在 Pandas 数据帧中,然后存储到 mysql 数据库和其他东西。问题是:网站的结果总是一样的!
这是我的代码:
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import time
def GetDataFromWeb(day, month, year):
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1920x1080')
#had to use these two below because of webdriver crashing issues
options.add_argument('no-sandbox')
options.add_argument('disable-dev-shm-usage')
driver = webdriver.Chrome(chrome_options=options)
driver.get("http://www.b3.com.br/pt_br/market-data-e-indices/servicos-de-dados/market-data/consultas/mercado-de-derivativos/precos-referenciais/taxas-referenciais-bm-fbovespa/")
#the table is on an iframe
iframe = driver.find_element_by_id("bvmf_iframe")
driver.switch_to.default_content()
driver.switch_to.frame(iframe)
#getting to the place where I should input the data
date = driver.find_element_by_id("Data")
date.send_keys("/".join((str(day),str(month),str(year))))
date = driver.find_element_by_tag_name("button").click()
#I have put this wait just to be sure it doesn't try to get info from an unloaded page
time.sleep(5)
page = bs(driver.page_source,"html.parser")
table = page.find(id='tb_principal1')
headers = ['Dias Corridos', '252','360']
matrix = []
for rows in table.select('tr')[2:]:
values = []
for columns in rows.select('td'):
values.append(columns.text.replace(',','.'))
matrix.append(values)
df = pd.DataFrame(data=matrix, columns=headers)
driver.close()
#only the first 2 columns are interesting for my purposes
return df.iloc[:,0:2]
无论我向它发送什么输入,这个函数产生的表总是相同的。它们似乎来自 06/09/2018(月 = 09,日 = 06)的相应日期。我认为主要问题是我不知道对他们数据库的查询是如何完成的,所以这总是像“默认日期”一样运行。我读过一些人谈论 Ajax 和 JavaScript 请求,但我不知道是不是这样。我怎么知道?
相关分类