如何使用 Python 查看星座运势?
使用 Python 查看星座运势
Python 已在全球应用于许多应用程序中,其中一项应用是使用 Python 语言的 Beautifulsoup 查看当日或次日的星座运势。以下是使用 Python 查看星座运势的步骤。
安装 beautifulsoup
首先,我们必须在 Python 工作环境中安装 beautifulsoup 包。以下为代码。
pip install bs4
安装 beautifulsoup 的输出如下。
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.
导入 requests
现在我们必须导入 requests 模块和 bs4 包。
import requests from bs4 import BeautifulSoup
定义代码
在此步骤中,我们必须定义使用 requests 和 BeautifulSoup 查看星座运势的代码。以下为代码。
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)
输出
以下是为定义的星座和日期预测的星座运势输出。
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.
示例
我们来看看另一个查看指定星座和日期的星座运势的示例。
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)
输出
以下是为定义的星座和日期生成的星座运势输出。
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.
广告