我正在嘗試使用 Selenium 將以下 DataFrame 發送到資本收益計算器網站 ( http://www.cgtcalculator.com/calculator.aspx )的文本欄位中。
B 01/12/2000 BARC 3000 4.82 15 72.3
B 02/12/2000 BARC 5000 4.825 15 120.62
B 03/09/2002 VOD 18000 3.040 10 273.60
B 15/01/2003 BP. 5000 3.750 10 93.75
B 24/03/2003 BP. 3000 3.820 10 57.30
S 14/04/2003 BARC 6000 5.520 15 0.00
S 23/02/2004 VOD 9000 3.620 10 0.00
S 24/02/2004 VOD 9000 3.625 10 0.00
S 15/07/2005 BP. 8000 6.280 10 0.00
B 22/01/2007 BP. 5000 5.500 10 124.20
B 22/06/2009 BP. 2000 5.020 10 50.20
S 24/12/2012 BP. 5000 4.336 10 0.00
到目前為止,我能夠分別發送每一列,但問題是這些列在文本欄位中出現在另一列之下。為了讓計算器正常作業,我需要維護 DataFrame 的格式。有沒有辦法做到這一點?
這是我到目前為止所做的:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
df = pd.read_csv("/Users/Test.csv")
driver = webdriver.Chrome()
driver.get("http://www.cgtcalculator.com/calculator.aspx")
inputElement = driver.find_element_by_id("TEXTAREA1")
inputElement.send_keys("\n".join(df. iloc[:, 0]),"\n")
inputElement.send_keys("\n".join(df. iloc[:, 1]),"\n")
inputElement.send_keys("\n".join(df. iloc[:, 2]),"\n")
inputElement.send_keys("\n".join(df. iloc[:, 3].astype(str)),"\n")
inputElement.send_keys("\n".join(df. iloc[:, 4].astype(str)),"\n")
inputElement.send_keys("\n".join(df. iloc[:, 5].astype(str)),"\n")
inputElement.send_keys("\n".join(df. iloc[:, 6].astype(str)),"\n")
driver.find_element_by_id("Button3").click()
driver.find_element_by_id("Button1").click()
uj5u.com熱心網友回復:
很容易將 df 發送到剪貼板并粘貼 w/selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
df = pd.DataFrame({'B': ['B', 'B', 'B', 'B', 'S', 'S', 'S', 'S', 'B', 'B', 'S'],
'01/12/2000': ['02/12/2000', '03/09/2002', '15/01/2003', '24/03/2003', '14/04/2003', '23/02/2004', '24/02/2004', '15/07/2005', '22/01/2007', '22/06/2009', '24/12/2012'],
'BARC': ['BARC', 'VOD', 'BP.', 'BP.', 'BARC', 'VOD', 'VOD', 'BP.', 'BP.', 'BP.', 'BP.'],
'3000': [5000, 18000, 5000, 3000, 6000, 9000, 9000, 8000, 5000, 2000, 5000],
'4.82': [4.825, 3.04, 3.75, 3.82, 5.52, 3.62, 3.625, 6.28, 5.5, 5.02, 4.336],
'15': [15, 10, 10, 10, 15, 10, 10, 10, 10, 10, 10],
'72.3': [120.62, 273.6, 93.75, 57.3, 0.0, 0.0, 0.0, 0.0, 124.2, 50.2, 0.0]})
driver = webdriver.Chrome()
driver.get("http://www.cgtcalculator.com/calculator.aspx")
inputElement = driver.find_element_by_id("TEXTAREA1")
df.to_clipboard(index=False, sep='\t')
inputElement.send_keys(Keys.CONTROL, 'v')
driver.find_element_by_id("Button1").click()
輸出
SUMMARY INFORMATION
Year CapitalGain Exemption UsePrevLosses TaperRelief ChargeableGain Tax*
-------------------------------------------------------------------------------------------
03-04 14175 7900 0 0 6275 1059
05-06 19848 8500 0 0 11348 2060
12-13 -5284 10600 0 0 0 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/377645.html
上一篇:在亞馬遜網頁產品上抓取ASIN
