1.获取内容
我们今天呢,就先做一个通过星座来得知三天的运势的小玩意,
这里有十二个星座,我点了第一个和第二个进去,也就是白羊座和金牛座:
就会发现一个规律
通过观察网址的链接,我这张丑脸泛起了灿烂的笑容。
也就是说,https://www.horoscope.com/us/horoscopes/general/是每个星座都共有的一段网址,
horoscope-general-daily-today.aspx?sign=1
我们只需改变today和sign={}对应的值就可以获取到每个星座对应的网址了
https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-today.aspx?sign=1
我们再打开金牛座的昨天的运势,发现daily-后面变成了tomorrow
2.代码
from bs4 import BeautifulSoup import requests 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") return soup.find("div", class_="main-horoscope").p.text#返回得到的内容——来自上天的指示
如果有小伙伴不知道自己的星座怎么办呢,所以我们就还需要一个函数去查询星座:
def check_sign():#得到星座 your_birth_day = input("输入您的生日的日期> ") your_birth_month = input("输入你生日的月份> ")
if (int(your_birth_month) == 12 and int(your_birth_day) >= 22) or ( int(your_birth_month) == 1 and int(your_birth_day) <= 19 ): sign = "Capricorn" elif (int(your_birth_month) == 1 and int(your_birth_day) >= 20) or ( int(your_birth_month) == 2 and int(your_birth_day) <= 17 ): sign = "Aquarium" elif (int(your_birth_month) == 2 and int(your_birth_day) >= 18) or ( int(your_birth_month) == 3 and int(your_birth_day) <= 19 ): sign = "Pices" elif (int(your_birth_month) == 3 and int(your_birth_day) >= 20) or ( int(your_birth_month) == 4 and int(your_birth_day) <= 19 ): sign = "Aries" elif (int(your_birth_month) == 4 and int(your_birth_day) >= 20) or ( int(your_birth_month) == 5 and int(your_birth_day) <= 20 ): sign = "Taurus" elif (int(your_birth_month) == 5 and int(your_birth_day) >= 21) or ( int(your_birth_month) == 6 and int(your_birth_day) <= 20 ): sign = "Gemini" elif (int(your_birth_month) == 6 and int(your_birth_day) >= 21) or ( int(your_birth_month) == 7 and int(your_birth_day) <= 22 ): sign = "Cancer" elif (int(your_birth_month) == 7 and int(your_birth_day) >= 23) or ( int(your_birth_month) == 8 and int(your_birth_day) <= 22 ): sign = "Leo" elif (int(your_birth_month) == 8 and int(your_birth_day) >= 23) or ( int(your_birth_month) == 9 and int(your_birth_day) <= 22 ): sign = "Virgo" elif (int(your_birth_month) == 9 and int(your_birth_day) >= 23) or ( int(your_birth_month) == 10 and int(your_birth_day) <= 22 ): sign = "Libra" elif (int(your_birth_month) == 10 and int(your_birth_day) >= 23) or ( int(your_birth_month) == 11 and int(your_birth_day) <= 21 ): sign = "Scorpio" elif (int(your_birth_month) == 11 and int(your_birth_day) >= 22) or ( int(your_birth_month) == 12 and int(your_birth_day) <= 21 ): sign = "Sagittarius"
PythonQUN:748,989,764
return sign
3.实操


怎么样?很有趣吧,当然网站有很多的用处,等以后我会继续更新,实现更多的好玩的功能。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示