24
loading...
This website collects cookies to deliver better user experience
$ pip install requests
$ pip install bs4
requests
and bs4
to scrape horoscope data from this website. The requests module allows us to send HTTP requests using Python. The HTTP request then returns a Response Object with all the responses of the webpage mentioned. We then use the BeautifulSoup library to pull data out of HTML and XML files. import requests
from bs4 import BeautifulSoup
def get_horoscope(zodiac_sign: int, day: str):
if not "-" in day:
res = requests.get(f"https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-{day}.aspx?sign={zodiac_sign}")
else:
day = day.replace("-", "")
res = requests.get(f"https://www.horoscope.com/us/horoscopes/general/horoscope-archive.aspx?sign={zodiac_sign}&laDate={day}")
soup = BeautifulSoup(res.content, 'html.parser')
data = soup.find('div', attrs={'class': 'main-horoscope'})
return data.p.text
get_horoscope()
function to print the horoscope data.if __name__ == ' __main__':
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
}
print(*zodiac_signs.keys(), sep=", ")
zodiac_symbol = input("Which is your zodiac sign from the above? : ").capitalize()
day = input("For which day do you wish to know the horoscope? For today, write 'today', for yesterday, write 'yesterday', for tomorrow, write 'tomorrow', else write the date in YYYY-MM-DD format : ")
print(f"Getting horoscope for {zodiac_symbol} for {day}\n")
print(get_horoscope(zodiac_signs[zodiac_symbol], day))
$ python main.py
Aries, Taurus, Gemini, Cancer, Leo, Virgo, Libra, Scorpio, Sagittarius, Capricorn, Aquarius, Pisces
Which is your zodiac sign from the above? : capricorn
For which day do you wish to know the horoscope? For today, write 'today', for yesterday, write 'yesterday', for tomorrow, write 'tomorrow', else write the date in YYYY-MM-DD format : today
Getting horoscope for Capricorn for today
Dec 6, 2021 - Things should be going your way, and you may feel like quite a bit is getting done without your having to lift a finger. At the same time, a nagging voice in the back of your mind is telling you to watch your back. Have all your bases covered. While you may be tempted to go with the most comfortable and familiar, this is a good time to consider other perspectives.
$ python main.py
Aries, Taurus, Gemini, Cancer, Leo, Virgo, Libra, Scorpio, Sagittarius, Capricorn, Aquarius, Pisces
Which is your zodiac sign from the above? : capricorn
For which day do you wish to know the horoscope? For today, write 'today', for yesterday, write 'yesterday', for tomorrow, write 'tomorrow', else write the date in YYYY-MM-DD format : 2021-12-01
Getting horoscope for Capricorn for 2021-12-01
Dec 1, 2021 - You may feel like a racehorse crammed in the starting gate. You're stuck in a small spot, stomping your feet, and anxiously waiting for the starting bell. You may feel powerless since you have no control over when the gate will open. Be patient. Don't waste all your energy fidgeting. The gate will open soon enough and you'll be up and running.