Python 如何检查星座

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.

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程