如何使用 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.

更新日期:09-08-2023

124 次瀏覽

開啟你的 職業生涯

完成課程獲得證書

開始
廣告