How to check horoscope using Python?


Checking horoscope using Python

Python is used in many applications all over the world. One of the applications is to check the horoscope on that day or day after using the Beautifulsoup of the python language. The following are the steps to be followed to check the horoscope using python.

Installing the beautifulsoup

Firstly, we have to install the beautifulsoup package in our python working envinornment. The below is the code.

pip install bs4

The output of installing the beautifulsoup is as follows.

Collecting bs4
  Downloading bs4-0.0.1.tar.gz (1.1 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .	
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .		
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .		
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .		
		
Installing collected packages: bs4
Successfully installed bs4-0.0.1
Note: you may need to restart the kernel to use updated packages.

Importing the requests

Now we have to import the requests module and bs4 package.

import requests
from bs4 import BeautifulSoup

Defining the code

In this step, we have to define the code to check the horoscope using the requests and BeautifulSoup.The following is the code.

from bs4 import BeautifulSoup
import requests
zodiac = {"Aries": 1,
   "Taurus": 2,
   "Gemini": 3,
   "Cancer": 4,
   "Leo": 5,
   "Virgo": 6,
   "Libra": 7,
   "Scorpio": 8,
   "Sagittarius": 9,
   'Capricorn': 10,
   "Aquarius": 11,
   "Pisces": 12}
sign_symbol = input("Enter zodiac sign:")
day = str(input("Enter the day of horoscope that you want to know — today/tomorrow/yesterday"))
result = requests.get(f"https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-{day}.aspx?sign={zodiac[sign_symbol]}")
b_soup = BeautifulSoup(result.content, 'html.parser')
result = b_soup.find("div", attrs={"class": 'main-horoscope'})
print("The horoscope of the",sign_symbol,"for",result.p.text)

Output

Following is the output of the horoscope predicted for the defined zodiac sign and day.

Enter zodiac sign:Virgo
Enter the day of horoscope that you want to know — today/tomorrow/yesterdaytomorrow
The horoscope of the Virgo for Apr 1, 2023 - Every day is a new start and new chance, Virgo. Don't get upset or angry over past events. Don't dwell on things you can't change. Your whole life can turn around in a day, so start every morning with a positive outlook. As you wash your face in the morning, think of it as a renewal. Clean off the debris from yesterday while welcoming the freshness of a new day.

Example

Let’s see another example of checking the horoscope of the given zodiac sign and defined day.

def horoscope(sign_symbol,day):
    sign_symbol = input("Enter zodiac sign: ")
    day = str(input("Enter the day of horoscope that you want to know — today/tomorrow/yesterday"))
    from bs4 import BeautifulSoup
    import requests
    zodiac = {  "Aries": 1,
       "Taurus": 2,
       "Gemini": 3,
       "Cancer": 4,
       "Leo": 5,
       "Virgo": 6,
       "Libra": 7,
       "Scorpio": 8,
       "Sagittarius": 9,
       'Capricorn': 10,
       "Aquarius": 11,
       "Pisces": 12}
    result = requests.get(f"https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-{day}.aspx?sign={zodiac[sign_symbol]}")
    b_soup = BeautifulSoup(result.content, 'html.parser')
    result = b_soup.find("div", attrs={"class": 'main-horoscope'})
    print("The horoscope of the",sign_symbol,"for",result.p.text)
horoscope(sign_symbol,day)

Output

Following is the output of the horoscope for the defined zodiac sign and day.

Enter zodiac sign: Gemini
Enter the day of horoscope that you want to know — today/tomorrow/yesterdaytoday
The horoscope of the Gemini for Mar 31, 2023 - Things may come to you more easily than usual today, Gemini. This is a sign that you're on the right path and doing things correctly. Life shouldn't have to be full of stress and heartache. If something isn't flowing smoothly, you should consider taking a new approach toward it. Keep a smile on your face and be respectful of the people around you.

Updated on: 09-Aug-2023

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements