寻址数据帧中某个值旁边的值

时间:2019-05-13 13:47:21

标签: python selenium

尝试寻址数据帧中的某个值。

我得到了一个如下所示的CSV文件: enter image description here

我将stock列用作for循环的索引。然后我使用硒从Yahoo Finance获得了历史价格数据,并将其转换为如下数据框: enter image description here 我想在从Yahoo Finance获得的数据框中搜索股票日期。

for stock_symbol in mystocks:
yahoo_url = "https://finance.yahoo.com/quote/"+stock_symbol+"/history?period1=0&period2=2597263000&interval=1d&filter=history&frequency=1d"

获取数据

bs_data = BeautifulSoup(source_data, features="lxml")
table = bs_data.find('table', attrs={"data-test": "historical-prices"})
dataf = pd.read_html(str(table))

我想创建一个for循环,从所需日期行中获取值并将其放入csv(股票名称,日期,开盘价,高价,低价,收盘价,收盘价,成交量)。

我想要的输出只是我得到的CSV中的某个日期。例如,在用于CALM的CSV中,所需日期为2019年4月1日,因此我只想从Yahoo数据框中提取此日期数据。

1 个答案:

答案 0 :(得分:1)

我不是BeautifulSoup方面的专家,因此我将csv格式的数据下载到默认目录(chrome选项)(有下载链接)对此颇为刺痛

在使用BeautifulSoup时,这绝不是您问题的答案,但是您可以考虑一下。

import time
from pathlib import Path

import pandas as pd
from selenium.webdriver import Remote
from selenium.webdriver.chrome.options import Options

download_path = Path(r'C:\stackoverflow')

options = Options()
options.add_experimental_option("prefs", {
    "download.default_directory": str(download_path),
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing.enabled": True
})

driver = Remote(options=options)
stock_symbols = ['CALM', 'CTRA', 'NVGS', 'ANGO']

for stock_symbol in stock_symbols:
    driver.get(f'https://finance.yahoo.com/quote/{stock_symbol}/history?period1=0&period2=2597263000&interval=1d&filter=history&frequency=1d')
    time.sleep(5) # Replace with Webdriver Wait

    download_data_link = driver.find_element_by_link_text('Download Data')
    file_name = download_data_link.get_attribute('download')
    download_data_link.click()

    file_path = download_path / file_name
    while True:
        if file_path.exists():
            break

    df = pd.DataFrame.from_csv(file_path)
    df['Stock Name'] = stock_symbol
    print(df.head())

    break

输出

           Open    High      Low     Close  Adj Close   Volume Stock Name
 Date                                                                         
 1996-12-12  1.81250  1.8125  1.68750  1.703125   0.743409  1984400       CALM
 1996-12-13  1.71875  1.8125  1.65625  1.781250   0.777510   996800       CALM
 1996-12-16  1.81250  1.8125  1.71875  1.718750   0.750229   122000       CALM
 1996-12-17  1.75000  1.8125  1.75000  1.773425   0.774094   239200       CALM
 1996-12-18  1.81250  1.8125  1.75000  1.812500   0.791151   216400       CALM

按日期过滤

df = df.reset_index()
print(df.loc[df['Date'] == '1996-12-12'])

        Date    Open    High     Low     Close  Adj Close   Volume Stock 
Name
0 1996-12-12  1.8125  1.8125  1.6875  1.703125   0.743409  1984400       CALM