1

我正在尝试编写代码以在输入行业名称时返回股票代码列表。

例如,MSFT 在 yfinance 的技术部门,我想要属于这个特定部门的其余公司。

import yfinance as yf

msft= yf.Ticker("MSFT")
print(msft.info['sector'])

此代码将返回“技术”,如何获取包含其他股票的数据框。

是否有可能获得比“技术”更具体的“通信”等更具体的部门类别?

4

2 回答 2

1

That data can be retrieved pretty easily with a package called yahooquery. Disclaimer: I am the author of the package.

To get stocks in the technology sector, you can do the following:

from yahooquery import Screener

s = Screener()

# data is a dictionary containing the keys passed to the function
data = s.get_screeners('ms_technology', count=25)

# the majority of the data will be in the quotes key
data['ms_technology']['quotes'][0]
{'language': 'en-US', 'region': 'US', 'quoteType': 'EQUITY', 'quoteSourceName': 'Delayed Quote', 'triggerable': True, 'currency': 'USD', 'priceHint': 2, 'longName': 'Apple Inc.', 'financialCurrency': 'USD', 'regularMarketOpen': 123.75, 'averageDailyVolume3Month': 106246233, 'averageDailyVolume10Day': 137149760, 'fiftyTwoWeekLowChange': 74.6375, 'fiftyTwoWeekLowChangePercent': 1.4042143, 'fiftyTwoWeekRange': '53.1525 - 145.09', 'fiftyTwoWeekHighChange': -17.299995, 'fiftyTwoWeekHighChangePercent': -0.119236305, 'fiftyTwoWeekLow': 53.1525, 'fiftyTwoWeekHigh': 145.09, 'dividendDate': 1613001600, 'earningsTimestamp': 1611765000, 'earningsTimestampStart': 1619607540, 'earningsTimestampEnd': 1620043200, 'trailingAnnualDividendRate': 0.807, 'trailingPE': 34.659615, 'trailingAnnualDividendYield': 0.0066551208, 'marketState': 'POSTPOST', 'epsTrailingTwelveMonths': 3.687, 'epsForward': 4.68, 'epsCurrentYear': 4.45, 'priceEpsCurrentYear': 28.716856, 'sharesOutstanding': 16788100096, 'bookValue': 3.936, 'fiftyDayAverage': 132.6306, 'fiftyDayAverageChange': -4.840599, 'fiftyDayAverageChangePercent': -0.036496848, 'twoHundredDayAverage': 122.9772, 'twoHundredDayAverageChange': 4.8127975, 'twoHundredDayAverageChangePercent': 0.039135687, 'marketCap': 2145351368704, 'forwardPE': 27.305557, 'priceToBook': 32.466972, 'sourceInterval': 15, 'exchangeDataDelayedBy': 0, 'exchangeTimezoneName': 'America/New_York', 'exchangeTimezoneShortName': 'EST', 'gmtOffSetMilliseconds': -18000000, 'esgPopulated': False, 'tradeable': True, 'firstTradeDateMilliseconds': 345479400000, 'postMarketChangePercent': 0.7434107, 'postMarketTime': 1614646799, 'postMarketPrice': 128.74, 'postMarketChange': 0.9500046, 'regularMarketChange': 6.529999, 'regularMarketTime': 1614632402, 'regularMarketPrice': 127.79, 'regularMarketDayHigh': 127.93, 'regularMarketDayRange': '122.79 - 127.93', 'regularMarketDayLow': 122.79, 'regularMarketVolume': 116307692, 'regularMarketPreviousClose': 121.26, 'bid': 128.74, 'ask': 128.75, 'bidSize': 10, 'askSize': 11, 'exchange': 'NMS', 'market': 'us_market', 'messageBoardId': 'finmb_24937', 'fullExchangeName': 'NasdaqGS', 'shortName': 'Apple Inc.', 'regularMarketChangePercent': 5.385122, 'displayName': 'Apple', 'symbol': 'AAPL'}

Put the data into a pandas DataFrame:

df = pd.DataFrame(data['ms_technology']['quotes'])

Retrieve multiple screeners at once:

data = s.get_screeners(['ms_technology', 'ms_utilities', 'ms_real_estate'])

Finally, view the list of available predefined screeners with the following:

# Will return a list
s.available_screeners
于 2021-03-02T02:51:23.803 回答
0

You're looking for 'industry' to get a more granular description:

msft= yf.Ticker("MSFT")
print(msft.info['industry'])

print(msft.info) to view the json file to see what is available. yFinance does not provide the same amount of information for all ticker symbols.

于 2021-03-02T01:33:48.640 回答