Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取Jupyter笔记本中的股票列表?_Python_Jupyter - Fatal编程技术网

Python 如何获取Jupyter笔记本中的股票列表?

Python 如何获取Jupyter笔记本中的股票列表?,python,jupyter,Python,Jupyter,编写代码,从维基百科获取所有标准普尔500指数股票的股票代码列表。截至2021年2月24日,该列表中有505个股票代码。只要代码实际查询以下网站以获取列表,您就可以使用任何方法: 一种方法是使用requests模块获取HTML代码,然后使用re模块提取ticker。另一个选项是pandas中的.read_html函数,然后将tickers列导出到列表中 您应该使用名称sp500_tickers将标记保存在列表中,这将获取名为“components”的表中的数据 # find a specifi

编写代码,从维基百科获取所有标准普尔500指数股票的股票代码列表。截至2021年2月24日,该列表中有505个股票代码。只要代码实际查询以下网站以获取列表,您就可以使用任何方法:

一种方法是使用requests模块获取HTML代码,然后使用re模块提取ticker。另一个选项是pandas中的.read_html函数,然后将tickers列导出到列表中


您应该使用名称sp500_tickers将标记保存在列表中,这将获取名为“components”的表中的数据

# find a specific table by table count
import pandas as pd
import requests
from bs4 import BeautifulSoup

res = requests.get("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")
soup = BeautifulSoup(res.content,'lxml')
table = soup.find_all('table')[0] 
df = pd.read_html(str(table))
print(df[0].to_json(orient='records'))
结果:

[{"Symbol":"MMM","Security":"3M Company","SEC filings":"reports","GICS Sector":"Industrials","GICS Sub-Industry":"Industrial Conglomerates","Headquarters Location":"St. Paul, Minnesota","Date first added":"1976-08-09","CIK":66740,"Founded":"1902"},{"Symbol":"ABT","Security":"Abbott Laboratories","SEC filings":"reports","GICS Sector":"Health Care","GICS Sub-Industry":"Health Care Equipment","Headquarters Location":"North Chicago, Illinois","Date first added":"1964-03-31","CIK":1800,"Founded":"1888"},{"Symbol":"ABBV","Security":"AbbVie Inc.","SEC filings":"reports","GICS Sector":"Health Care","GICS Sub-Industry":"Pharmaceuticals","Headquarters Location":"North Chicago, Illinois","Date first added":"2012-12-31","CIK":1551152,"Founded":"2013 (1888)"},{"Symbol":"ABMD","Security":"Abiomed","SEC filings":"reports","GICS Sector":"Health Care","GICS Sub-Industry":"Health Care Equipment","Headquarters Location":"Danvers, Massachusetts","Date first added":"2018-05-31","CIK":815094,"Founded":"1981"},{"Symbol":"ACN","Security":"Accenture","SEC filings":"reports","GICS Sector":"Information Technology","GICS Sub-Industry":"IT Consulting & Other Services","Headquarters Location":"Dublin, Ireland","Date first added":"2011-07-06","CIK":1467373,"Founded":"1989"},{"Symbol":"ATVI","Security":"Activision Blizzard","SEC filings":"reports","GICS Sector":"Communication Services","GICS Sub-Industry":"Interactive Home Entertainment","Headquarters Location":"Santa Monica, California","Date first added":"2015-08-31","CIK":718877,"Founded":"2008"},{"Symbol":"ADBE","Security":"Adobe Inc.","SEC filings":"reports","GICS Sector":"Information Technology","GICS Sub-Industry":"Application Software","Headquarters Location":"San Jose, California","Date first added":"1997-05-05","CIK":796343,"Founded":"1982"},
Etc., etc., etc.
[    Symbol             Security SEC filings             GICS Sector  \
 0      MMM           3M Company     reports             Industrials   
 1      ABT  Abbott Laboratories     reports             Health Care   
 2     ABBV          AbbVie Inc.     reports             Health Care   
 3     ABMD              Abiomed     reports             Health Care   
 4      ACN            Accenture     reports  Information Technology   
 ..     ...                  ...         ...                     ...   
 500    YUM      Yum! Brands Inc     reports  Consumer Discretionary   
 501   ZBRA   Zebra Technologies     reports  Information Technology   
 502    ZBH        Zimmer Biomet     reports             Health Care   
 503   ZION        Zions Bancorp     reports              Financials   
 504    ZTS               Zoetis     reports             Health Care   
 
                       GICS Sub-Industry    Headquarters Location  \
 0              Industrial Conglomerates      St. Paul, Minnesota   
 1                 Health Care Equipment  North Chicago, Illinois   
 2                       Pharmaceuticals  North Chicago, Illinois   
 3                 Health Care Equipment   Danvers, Massachusetts   
 4        IT Consulting & Other Services          Dublin, Ireland   
 ..                                  ...                      ...   
 500                         Restaurants     Louisville, Kentucky   
 501  Electronic Equipment & Instruments   Lincolnshire, Illinois   
 502               Health Care Equipment          Warsaw, Indiana   
 503                      Regional Banks     Salt Lake City, Utah   
 504                     Pharmaceuticals   Parsippany, New Jersey   
 
     Date first added      CIK      Founded  
 0         1976-08-09    66740         1902  
 1         1964-03-31     1800         1888  
 2         2012-12-31  1551152  2013 (1888)  
 3         2018-05-31   815094         1981  
 4         2011-07-06  1467373         1989  
 ..               ...      ...          ...  
 500       1997-10-06  1041061         1997  
 501       2019-12-23   877212         1969  
 502       2001-08-07  1136869         1927  
 503       2001-06-22   109380         1873  
 504       2013-06-21  1555280         1952  
 
 [505 rows x 9 columns]]
这就是JSON。如果您想要一个类似于Excel中使用的表格,只需打印
df

结果:

[{"Symbol":"MMM","Security":"3M Company","SEC filings":"reports","GICS Sector":"Industrials","GICS Sub-Industry":"Industrial Conglomerates","Headquarters Location":"St. Paul, Minnesota","Date first added":"1976-08-09","CIK":66740,"Founded":"1902"},{"Symbol":"ABT","Security":"Abbott Laboratories","SEC filings":"reports","GICS Sector":"Health Care","GICS Sub-Industry":"Health Care Equipment","Headquarters Location":"North Chicago, Illinois","Date first added":"1964-03-31","CIK":1800,"Founded":"1888"},{"Symbol":"ABBV","Security":"AbbVie Inc.","SEC filings":"reports","GICS Sector":"Health Care","GICS Sub-Industry":"Pharmaceuticals","Headquarters Location":"North Chicago, Illinois","Date first added":"2012-12-31","CIK":1551152,"Founded":"2013 (1888)"},{"Symbol":"ABMD","Security":"Abiomed","SEC filings":"reports","GICS Sector":"Health Care","GICS Sub-Industry":"Health Care Equipment","Headquarters Location":"Danvers, Massachusetts","Date first added":"2018-05-31","CIK":815094,"Founded":"1981"},{"Symbol":"ACN","Security":"Accenture","SEC filings":"reports","GICS Sector":"Information Technology","GICS Sub-Industry":"IT Consulting & Other Services","Headquarters Location":"Dublin, Ireland","Date first added":"2011-07-06","CIK":1467373,"Founded":"1989"},{"Symbol":"ATVI","Security":"Activision Blizzard","SEC filings":"reports","GICS Sector":"Communication Services","GICS Sub-Industry":"Interactive Home Entertainment","Headquarters Location":"Santa Monica, California","Date first added":"2015-08-31","CIK":718877,"Founded":"2008"},{"Symbol":"ADBE","Security":"Adobe Inc.","SEC filings":"reports","GICS Sector":"Information Technology","GICS Sub-Industry":"Application Software","Headquarters Location":"San Jose, California","Date first added":"1997-05-05","CIK":796343,"Founded":"1982"},
Etc., etc., etc.
[    Symbol             Security SEC filings             GICS Sector  \
 0      MMM           3M Company     reports             Industrials   
 1      ABT  Abbott Laboratories     reports             Health Care   
 2     ABBV          AbbVie Inc.     reports             Health Care   
 3     ABMD              Abiomed     reports             Health Care   
 4      ACN            Accenture     reports  Information Technology   
 ..     ...                  ...         ...                     ...   
 500    YUM      Yum! Brands Inc     reports  Consumer Discretionary   
 501   ZBRA   Zebra Technologies     reports  Information Technology   
 502    ZBH        Zimmer Biomet     reports             Health Care   
 503   ZION        Zions Bancorp     reports              Financials   
 504    ZTS               Zoetis     reports             Health Care   
 
                       GICS Sub-Industry    Headquarters Location  \
 0              Industrial Conglomerates      St. Paul, Minnesota   
 1                 Health Care Equipment  North Chicago, Illinois   
 2                       Pharmaceuticals  North Chicago, Illinois   
 3                 Health Care Equipment   Danvers, Massachusetts   
 4        IT Consulting & Other Services          Dublin, Ireland   
 ..                                  ...                      ...   
 500                         Restaurants     Louisville, Kentucky   
 501  Electronic Equipment & Instruments   Lincolnshire, Illinois   
 502               Health Care Equipment          Warsaw, Indiana   
 503                      Regional Banks     Salt Lake City, Utah   
 504                     Pharmaceuticals   Parsippany, New Jersey   
 
     Date first added      CIK      Founded  
 0         1976-08-09    66740         1902  
 1         1964-03-31     1800         1888  
 2         2012-12-31  1551152  2013 (1888)  
 3         2018-05-31   815094         1981  
 4         2011-07-06  1467373         1989  
 ..               ...      ...          ...  
 500       1997-10-06  1041061         1997  
 501       2019-12-23   877212         1969  
 502       2001-08-07  1136869         1927  
 503       2001-06-22   109380         1873  
 504       2013-06-21  1555280         1952  
 
 [505 rows x 9 columns]]
或者,您可以将df导出为CSV文件

df.to_csv('constituents.csv')

这本是从家庭作业中粘贴出来的吗?尝试一下,如果你遇到了错误,发布一些细节,包括相关代码和错误信息。请解释你的问题,不要复制粘贴你的作业。请展示您为解决此问题而编写的工作代码和有关您的试用的示例数据,并提供和复制您希望获得帮助的特定错误消息或详细信息。因此,我们可以复制粘贴和复制,这样更容易回答您的问题。请参见此处的MRE: