首页 > 解决方案 > 如何在 Python 中使用 Pandas DF 值作为字符串,以便我可以在 Selenium 中发送具有从 Pandas DF 中提取的确切值的密钥?

问题描述

所以我有一个包含股票代码和价格的 csv 文件。我创建了一个 webscraper 来与我的“Home-Broker”交互,因为我还不知道如何处理 websocket。

我想要做的是使用 Pandas 从 csv 文件中获取符号和价格,并使用 selenium 到 .sendkeys 以及每个特定表单上的符号和价格。

Bellow 是我的 csv 中 df.head(3) 的输出示例。

      Symbol   Price
    0  APPL    319.61
    1  GOOG    1,508.79
    2  AMZN    2,150.80

这是我用来在我的家庭经纪人上发送密钥的输入,我正在手动更改每只股票的代码。

driver.find_element_by_xpath('//*[@id="txtAtivo_BOOK1"]').click()
driver.find_element_by_xpath('//*[@id="txtAtivo_BOOK1"]').send_keys('APPL')
driver.find_element_by_xpath('//*[@id="txtAtivo_BOOK1"]').send_keys(Keys.ENTER)
time.sleep(1)
driver.find_element_by_xpath('//*[@id="txtAssElet_ORDERS1"]').send_keys('319.61')
time.sleep(1)
driver.find_element_by_xpath('//*[@id="DV_barraCompraVende_BOOK1"]/table[3]/tbody/tr/td[3]').click()
time.sleep(10)

我需要将 .send_keys('APPL') 和 .send_keys('319.61') 替换为某种 .send_keys df.head(0) 并循环直到我发送 df.head(3) 的购买订单

有没有办法做到这一点?

感谢您的耐心等待,并提前感谢您

标签: pythonpandasseleniumdataframestock

解决方案


如果你只需要抓住股价,你可以这样做。

import datetime
import pandas as pd
import numpy as np
import pylab as pl
import datetime
from sklearn.cluster import AffinityPropagation
from sklearn import metrics
from matplotlib.collections import LineCollection
from pandas_datareader import data as wb
from sklearn import cluster, covariance, manifold


start = '2019-02-01'
end = '2020-02-01'

tickers = ['MMM',
'ABT',
'ABBV',
'ABMD',
'ACN',
'ATVI']

thelen = len(tickers)

price_data = []
for ticker in tickers:
    prices = wb.DataReader(ticker, start = start, end = end, data_source='yahoo')[['Open','Adj Close']]
    price_data.append(prices.assign(ticker=ticker)[['ticker', 'Open', 'Adj Close']])

#names = np.reshape(price_data, (len(price_data), 1))

names = pd.concat(price_data)
names.reset_index()


################################
### OR...for a different layout...

thelen = len(tickers)

price_data = []
for ticker in tickers:
    prices = wb.DataReader(ticker, start = start, end = end, data_source='yahoo')[['Adj Close']]
    price_data.append(prices.assign(ticker=ticker)[['ticker', 'Adj Close']])

df = pd.concat(price_data)
df.dtypes
df.head()
df.shape

pd.set_option('display.max_columns', 500)

df = df.reset_index()
df = df.set_index('Date')
table = df.pivot(columns='ticker')
# By specifying col[1] in below list comprehension
# You can select the stock names under multi-level column
table.columns = [col[1] for col in table.columns]
table.head()

推荐阅读