How to check horoscope using Python ?
Last Updated :
18 Jul, 2021
In this article, we are going to see how to get a horoscope a day before, on that day as well as the day after using Beautifulsoup.
Module needed:
- bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This module does not come built-in with Python. To install this type the command below in the terminal.
pip install bs4
- requests: Request allows you to send HTTP/1.1 requests extremely easily. This module also does not come built-in with Python. To install this type the command below in the terminal.
pip install requests
Stepwise implementation:
Step 1: Importing Modules
The requests module allows you to send HTTP requests using Python. The HTTP request returns a Response Object with all the responses of the webpage mentioned and BeautifulSoup is a Python library for pulling data out of HTML and XML files.
Python3
import requests
from bs4 import BeautifulSoup
|
Step 2: Defining the “horoscope” function
This function will take two variables as input “zodiac_sign” and “day” which will be the user’s specified zodiac sign and day on which the user wants to know their horoscope. This will then be fed to the website url, which in our case, will be www.horoscope.com in the “{day}” and “{zodiac_sign}” sections respectively.
This will make sure that the data we want is from the specified day and zodiac sign input by the user. After that, an HTTP request will be sent to the website and with the help of beautiful soup we will pull out the data from the website’s HTML file.
Then after some navigation, we find out that the horoscope data which we require is present in the “main-horoscope” class, which we will find from soup.find() function and after extracting the paragraph text string, we will simply return it in the string format.
Python3
import requests
from bs4 import BeautifulSoup
def horoscope(zodiac_sign: int , day: str ) - > str :
url = (
f "horoscope-general-daily-{day}.aspx?sign={zodiac_sign}"
)
soup = BeautifulSoup(requests.get(url).content,
"html.parser" )
return soup.find( "div" , class_ = "main-horoscope" ).p.text
|
Output:
Here we have to use “main-horoscope” div class
Note: This is only HTML code or Raw data.
Step 3: Defining the main function
Firstly, we will store all the zodiac sign’s strings as key and a specific number as their value in a dictionary. Then we will ask the user to input their zodiac sign which will give a number from our dictionary and store it as “zodiac_sign” and similarly, we will store day in our “day” variable. This will then be pushed to the horoscope function, which will generate a string that we return at the last. This string will be the horoscope as told by the website.
Python3
if __name__ = = "__main__" :
dic = { 'Aries' : 1 , 'Taurus' : 2 , 'Gemini' : 3 ,
'Cancer' : 4 , 'Leo' : 5 , 'Virgo' : 6 , 'Libra' : 7 ,
'Scorpio' : 8 , 'Sagittarius' : 9 , 'Capricorn' : 10 ,
'Aquarius' : 11 , 'Pisces' : 12 }
print ( 'Choose your zodiac sign from below list : \n' ,
'[Aries,Taurus,Gemini,Cancer,Leo,Virgo,Libra,\
Scorpio,Sagittarius,Capricorn,Aquarius,Pisces]')
zodiac_sign = dic[ input ( "Input your zodiac sign : " )]
print ( "On which day you want to know your horoscope ?\n" ,
"Yesterday\n" , "Today\n" , "Tomorrow\n" )
day = input ( "Input the day : " ).lower()
horoscope_text = horoscope(zodiac_sign, day)
print (horoscope_text)
|
Below is the full implementation:
Python3
import requests
from bs4 import BeautifulSoup
def horoscope(zodiac_sign: int , day: str ) - > str :
url = (
f "horoscope-general-daily-{day}.aspx?sign={zodiac_sign}"
)
soup = BeautifulSoup(requests.get(url).content,
"html.parser" )
return soup.find( "div" , class_ = "main-horoscope" ).p.text
if __name__ = = "__main__" :
dic = { 'Aries' : 1 , 'Taurus' : 2 , 'Gemini' : 3 ,
'Cancer' : 4 , 'Leo' : 5 , 'Virgo' : 6 ,
'Libra' : 7 , 'Scorpio' : 8 , 'Sagittarius' : 9 ,
'Capricorn' : 10 , 'Aquarius' : 11 , 'Pisces' : 12 }
print ( 'Choose your zodiac sign from below list : \n' ,
'[Aries,Taurus,Gemini,Cancer,Leo,Virgo,Libra,\
Scorpio,Sagittarius,Capricorn,Aquarius,Pisces]')
zodiac_sign = dic[ input ( "Input your zodiac sign : " )]
print ( "On which day you want to know your horoscope ?\n" ,
"Yesterday\n" , "Today\n" , "Tomorrow\n" )
day = input ( "Input the day : " ).lower()
horoscope_text = horoscope(zodiac_sign, day)
print (horoscope_text)
|
Output: