Skip to main content

Get Your Horoscope Using Python

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


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 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:

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“{day}.aspx?sign={zodiac_signs[zodiac_symbol]})
soup = BeautifulSoup(result.content, ‘html.parser’) data = soup.find(‘div’, attrs={class: ‘main-horoscope’})

Executing above code will the display horoscope for you.

Don't forget to check out the recorded video of this utility on my YouTube channel named Shweta Lodha.