Add Search Utility
Generalizes isin search to support new types of search queries.
Use case:
I would like to be able to keep all yahoo finance code contained in the yfinance lib. Right now to perform a ticker search, I have to perform the following in my application:
response = requests.get(
"https://query1.finance.yahoo.com/v1/finance/search",
params={"q": q, "newsCount": 0, "quotesCount": 5, "enableFuzzyQuery": False},
headers={
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
},
)
Related to https://github.com/ranaroussi/yfinance/issues/837
Give an example use code, so people can try out.
Sorry, by example use code do you mean providing a snippet here or adding to the README?
In either case, here's a quick snippet.
import json
from yfinance.utils import search
user_input = input("Enter a ticker query: ")
search_result = search(user_input, newsCount=0) # Just search for tickers
search_result_quotes = search_result.get("quotes", [])
print(json.dumps(search_result_quotes, indent=2)) # Pretty print
Input
Enter a ticker query: apple
Output
[
{
"exchange": "NMS",
"shortname": "Apple Inc.",
"quoteType": "EQUITY",
"symbol": "AAPL",
...
},
...
]
Just so I understand the use case correctly - how is this different to Ticker.news? Should Ticker.news be changed to use your code.
This use case targets searching for tickers - the underlying API does also fetch related news articles, but the primary motivation behind this is some kind of ticker search functionality. I should also note that I haven't found anything in the library currently for this use case, but I could be missing something. Hope that clears this up!
https://github.com/ranaroussi/yfinance/blob/930b305327e2e3769b5d62115b3ab25bc58f28de/yfinance/base.py#L502
@ValueRaider, the get_news function currently only returns the news list and excludes the quotes list in the /search response. Having a search utility function would be a valuable addition and filtering the data to show just the quotes would help eliminate redundancy with get_news.
would help eliminate redundancy with
get_news
Agree. I wasn't rejecting the idea.