Search anything on Yahoo: web pages, news, videos, autocomplete, and weather.
$ pip install yahoo-search-py
Simple and Pythonic API with Pydantic.
Get almost every result: also_try
, pages
, card
, and related_searches
.
from yahoo_search import search
result = search("chocolate")
print(result.pages[0])
See Example Output
PageResult(
title='Chocolate - Wikipedia',
link='https://en.wikipedia.org/wiki/Chocolate',
text='A "cocoa product" is defined as a food product that is sourced from cocoa beans and contains "cocoa nibs, cocoa liquor, cocoa mass, unsweetened chocolate, bitter chocolate, chocolate liquor, cocoa, low-fat cocoa, cocoa powder, or low-fat cocoa powder". Conching. Main article: Conching.'
)
You can also search news with ease.
from yahoo_search import search_news
result = search_news("taiwan")
print(result.news[0])
See Example Output
News(
title='How to take the Taiwan Design Expo personality test that\'s trending on IG Stories',
thumbnail='https://s.yimg.com/fz/api/res/1.2/(...)',
source='Lifestyle Asia via Yahoo Style Singapore',
text='If you\'ve always wanted to know your personality type beyond the conventional MBTI variants, it\'s...'
)
Search and preview online videos easily.
from yahoo_search import search_videos
result = search_videos("jvke - golden hour")
print(result.videos[0].video_preview)
See Example Output
https://tse3.mm.bing.net/th?id=OM.7UQt_nfsv8nF0A_1687237113&pid=Api
Get the weather, because why not.
from yahoo_search import weather
w = weather()
print(w.celsius)
print(w.forecast["Monday"])
See Example Output
30
WeatherForecast(
fahrenheit=HighLowTemperature(
highest=92,
lowest=78
),
celsius=HighLowTemperature(
highest=34,
lowest=26
),
weather=WeatherForecastInner(
text='Haze',
icon='https://s.yimg.com/g/images/spaceball.gif'
),
precipitation=Precipitation(
icon='https://s.yimg.com/g/images/spaceball.gif',
percentage='0%'
)
)
You can add autocompletes to your applications.
from yahoo_search import autocomplete
print(autocomplete("hello"))
See Example Output
["hello fresh", "hello kitty", "hello molly", "hello neighbor", "hello october", "hello fresh log in to my account", "hello october images", "hello kitty coloring pages", "hello magazine", "hellosign"]
Below are some simple examples (and inspirations):
Below is a simple app that implements Yahoo searching right in your terminal.
from yahoo_search import search
while True:
query = input("search: ")
result = search(query)
if result.card:
# if there's a wikipedia definition
print("meaning", result.card.heading)
print(result.card.text)
for page in result.pages:
print(page.title, page.link)
print(page.text)
for search in result.related_searches:
print("related search: ", search.text)
Tired of "hourly" or "daily" news? Try minutely, and let them fill into your mind... full of news.
import time
from yahoo_search import search_news
keywords = ("news", "taiwan", "usa", "chocolate")
current = 0
while True:
result = search_news(keywords[current])
for news in result.news:
print(news.title)
print(news.text)
print()
# loop through the keywords
current += 1
current %= len(keywords)
time.sleep(60)
We love public APIs, so let's make our own.
import fastapi
from yahoo_search import search_videos
app = fastapi.FastAPI()
@app.get("/preview")
def preview_video(query: str):
# takes the URL param
res = search_videos(query)
return {
"url": res.videos[0].video_preview
}
Nawh, I ain't gonna setup a whole Flask app for this 💀💀
Terminal app is enough.
from yahoo_search import weather
res = weather()
print("Forecast")
for day, forecast in res.forecast.items():
print(
day,
"-",
forecast.weather.text,
forecast.precipitation.percentage,
forecast.fahrenheit.highest,
"/",
forecast.fahrenheit.lowest
)
If you're working with coroutines, such as Discord bots or FastAPI, you can wrap it into async.
(The below code structure is from Stackoverflow.)
import asyncio
from yahoo_search import search
async def asearch(loop, query):
# None uses the default executor (ThreadPoolExecutor)
await loop.run_in_executor(
None,
search,
query
)
Below are the models & functions type definitions.
See All Models (15)
class AlsoTryItem(BaseModel):
link: str
text: str
class PageResult(BaseModel):
title: str
link: str
text: Optional[str] = None
class CardResultSource(BaseModel):
link: str
text: str
class CardResult(BaseModel):
image: Optional[str] = None
heading: Optional[str] = None
text: Optional[str] = None
source: Optional[CardResultSource] = None
class RelatedSearch(BaseModel):
link: str
text: str
class SearchResult(BaseModel):
also_try: List[AlsoTryItem]
pages: List[PageResult]
card: Optional[CardResult] = None
related_searches: List[RelatedSearch]
class News(BaseModel):
title: str
thumbnail: Optional[str] = None
source: Optional[str] = None
last_updated: Optional[str] = None
text: Optional[str] = None
class NewsSearchResult(BaseModel):
news: List[News]
class Video(BaseModel):
age: Optional[str] = None
cite: Optional[str] = None
thumbnail: Optional[str] = None
video_preview: Optional[str] = None
title: str
link: str
class VideoSearchResult(BaseModel):
videos: List[Video]
class HighLowTemperature(BaseModel):
highest: int
lowest: int
class WeatherForecastInner(BaseModel):
text: str
icon: str
class Precipitation(BaseModel):
icon: str
percentage: str
class WeatherForecast(BaseModel):
fahrenheit: HighLowTemperature
celsius: HighLowTemperature
weather: WeatherForecastInner
precipitation: Precipitation
class WeatherInformation(BaseModel):
location: str
country: str
time: str
celsius: int
fahrenheit: int
weather: str
weather_icon: str
forecast: Dict[
Literal[
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
],
WeatherForecast
]
See All Functions (4)
def search(query: str) -> SearchResult: ...
def search_news(query: str) -> NewsSearchResult: ...
def weather() -> WeatherInformation: ...
def autocomplete(query: str) -> List[str]: ...
No, this time I'm not bored. I just want to practice webscraping.