免责声明:初学者 如果已经发布了重复的问题,将删除
我正在寻找一个脚本,该脚本将从多个网页中获取数据并将其存储为数据框中的列。 正如您从下面看到的那样,我可以成功获取一只股票的数据,我想知道是否有人想修改它,以便可以使用
之类的东西。stocklist = ["AMZN", "GOOG", "TSLA"]
以下脚本:
from time import sleep
from selenium import webdriver
import pandas as pd
driver = webdriver.Chrome('/chromedriver')
stock_list = ['AMZN']
values = []
metrics = []
def stocks():
for i in stock_list:
driver.get(f"http://finviz.com/quote.ashx?t={i}")
value = driver.find_elements_by_xpath("//td[@class='snapshot-td2']")
metric = driver.find_elements_by_xpath("//td[@class='snapshot-td2-cp']")
for i in metric:
metrics.append(i.text)
for a in value:
values.append(a.text)
def frames():
d = pd.DataFrame({'Metrics': metrics,'AMZN': values})
print(d)
d.to_csv("AMZN.csv")
理想情况下,您想为每个新股票及其对应的值创建一列。 当前输出如下:
Metrics AMZN
0 Index S&P 500
1 P/E 116.67
2 EPS (ttm) 20.93
3 Insider Own 11.20%
4 Shs Outstand 498.00M
.. ... ...
67 SMA20 2.96%
68 SMA50 10.13%
69 SMA200 27.34%
70 Volume 689,073
71 Change 0.93%
答案 0 :(得分:0)
这很简单:
from selenium import webdriver
from time import sleep
from selenium import webdriver
import pandas as pd
driver = webdriver.Chrome(executable_path=r"/chromedriver")
df = pd.DataFrame()
stock_list = ["AMZN", "GOOG", "TSLA"]
for stock in stock_list:
values = []
metrics = []
driver.get(f"http://finviz.com/quote.ashx?t={stock}")
driver.implicitly_wait(10)
page_metrics = driver.find_elements_by_xpath("//td[@class='snapshot-td2-cp']")
for metric in page_metrics:
metrics.append(metric.text)
page_values = driver.find_elements_by_xpath("//td[@class='snapshot-td2']")
for value in page_values:
values.append(value.text)
metric_column = 'Metrics_'+stock
df[metric_column] = metrics
df[stock] = values
df.to_csv("finviz.csv")
只需遍历您将理解的代码即可。这是在工作表中输出的:
我只剩下一个部分供您找出并解决-我正在为'Metrics'
写each stock
列-如果您需要,可以将其更改为单列。