If you are a person who believes in horoscopes and also know Python, then this article is for you. Horoscope is a way to forecast future. In this article, I’ll show you how to get your horoscope based on your zodiac sign.
Required Packages
Beautiful soup
We will use this to extract data out of HTML and can be installed as shown below:
pip install bs4
Requests
We will use this to make HTTP call and can be installed as shown below:
pip install requests
Website For Reading Horoscope
For this article, I’m using a website named https://www.horoscope.com for extracting horoscope information and it looks like this:
From above page, you can select your zodiac sign and select the day. Doing this will generate an URL similar to this: https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-tomorrow.aspx?sign=9
In this URL, tomorrow represents the day for which I’m looking for horoscope and 9 represents the zodiac sign. Yes, you guessed it correct. I queried for tomorrow’s data for Sagittarius sign.
Now, in order to fetch the horoscope programmatically, we just need to replace these two values while making a call.
Code To Get Horoscope
zodiac_signs = {
“Aries”: 1,
“Taurus”: 2,
“Gemini”: 3,
“Cancer”: 4,
“Leo”: 5,
“Virgo”: 6,
“Libra”: 7,
“Scorpio”: 8,
“Sagittarius”: 9,
“Capricorn”: 10,
“Aquarius”: 11,
“Pisces”: 12}
zodiac_symbol = input(“Enter zodiac sign:”)
day = input(“For which day do you wish to know the horoscope — today/tomorrow/yesterday”)
result = requests.get(f“https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-{day}.aspx?sign={zodiac_signs[zodiac_symbol]}”)
soup = BeautifulSoup(result.content, ‘html.parser’) data = soup.find(‘div’, attrs={‘class’: ‘main-horoscope’})
print(data.p.text)
Executing above code will display the horoscope for you.
In case, if you want to look at recorded video of this article, you can check out here on my YouTube channel.
Happy coding!