如何使用Python检查星座?
在本文中,我们将了解如何在使用 Beautifulsoup 的前一天、当天和后一天获得星座运势。
需要的模块:
- bs4 : Beautiful Soup(bs4) 是一个Python库,用于从 HTML 和 XML 文件中提取数据。这个模块没有内置于Python。要安装此类型,请在终端中输入以下命令。
pip install bs4
- requests : Request 允许您非常轻松地发送 HTTP/1.1 请求。这个模块也没有内置于Python。要安装此类型,请在终端中输入以下命令。
pip install requests
分步实施:
步骤 1:导入模块
requests 模块允许您使用Python发送 HTTP 请求。 HTTP 请求返回一个响应对象,其中包含提到的网页的所有响应,而 BeautifulSoup 是一个Python库,用于从 HTML 和 XML 文件中提取数据。
Python3
import requests
from bs4 import BeautifulSoup
Python3
# importing necessary modules
import requests
from bs4 import BeautifulSoup
def horoscope(zodiac_sign: int, day: str) -> str:
# website taking the user input variables
url = (
"https://www.horoscope.com/us/horoscopes/general/"
f"horoscope-general-daily-{day}.aspx?sign={zodiac_sign}"
)
# soup will contain all the website's data
soup = BeautifulSoup(requests.get(url).content,
"html.parser")
# print(soup)
# we will search for main-horoscope
# class and we will simply return it
return soup.find("div", class_="main-horoscope").p.text
Python3
if __name__ == "__main__":
# dictionary for storing all zodiac signs
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}
# asking for user's input
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()
# the data will be sent to the horoscope function
horoscope_text = horoscope(zodiac_sign, day)
# then we will simply print the resulting string
print(horoscope_text)
Python3
import requests
from bs4 import BeautifulSoup
def horoscope(zodiac_sign: int, day: str) -> str:
url = (
"https://www.horoscope.com/us/horoscopes/general/"
f"horoscope-general-daily-{day}.aspx?sign={zodiac_sign}"
)
soup = BeautifulSoup(requests.get(url).content,
"html.parser")
# print(soup.find("div", class_="main-horoscope").p.text)
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)
第 2 步:定义“星座”函数
该函数将采用两个变量作为输入“zodiac_sign”和“day”,这将是用户指定的星座和用户想知道他们的星座的日子。然后这将被馈送到网站 url,在我们的例子中,将分别是“{day}”和“{zodiac_sign}”部分中的 www.horoscope.com。
这将确保我们想要的数据来自用户输入的指定日期和星座。之后,一个 HTTP 请求将被发送到该网站,在美丽汤的帮助下,我们将从网站的 HTML 文件中提取数据。
然后经过一些导航,我们发现我们需要的星座数据存在于“main-horoscope”类中,我们将从soup.find()函数找到它,提取段落文本字符串,我们将简单地返回它以字符串格式。
蟒蛇3
# importing necessary modules
import requests
from bs4 import BeautifulSoup
def horoscope(zodiac_sign: int, day: str) -> str:
# website taking the user input variables
url = (
"https://www.horoscope.com/us/horoscopes/general/"
f"horoscope-general-daily-{day}.aspx?sign={zodiac_sign}"
)
# soup will contain all the website's data
soup = BeautifulSoup(requests.get(url).content,
"html.parser")
# print(soup)
# we will search for main-horoscope
# class and we will simply return it
return soup.find("div", class_="main-horoscope").p.text
输出:
![](https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/How_to_check_horoscope_using_Python_?_0.jpg)
这里我们必须使用“主星座”div类
注意:这只是 HTML 代码或原始数据。
第三步:定义主函数
首先,我们将所有十二生肖的字符串作为键存储在一个字典中,一个特定的数字作为它们的值。然后我们将要求用户输入他们的星座,它会从我们的字典中给出一个数字并将其存储为“zodiac_sign”,同样,我们将在“day”变量中存储日期。然后将其推送到 horoscope函数,该函数将生成我们最后返回的字符串。该字符串将是网站所告知的星座。
蟒蛇3
if __name__ == "__main__":
# dictionary for storing all zodiac signs
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}
# asking for user's input
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()
# the data will be sent to the horoscope function
horoscope_text = horoscope(zodiac_sign, day)
# then we will simply print the resulting string
print(horoscope_text)
下面是完整的实现:
蟒蛇3
import requests
from bs4 import BeautifulSoup
def horoscope(zodiac_sign: int, day: str) -> str:
url = (
"https://www.horoscope.com/us/horoscopes/general/"
f"horoscope-general-daily-{day}.aspx?sign={zodiac_sign}"
)
soup = BeautifulSoup(requests.get(url).content,
"html.parser")
# print(soup.find("div", class_="main-horoscope").p.text)
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)
输出: