python爬虫竟然被小伙用来算命
作者:弈鸣敲代码 时间:2023-08-28 17:35:20
前言
相信在日常生活中,平常大家聚在一起总会聊聊天,特别是女生(有冒犯到doge)非常喜欢聊星座,这个男生什么星座呀,那个男生什么星座呀…今天我就来满足各位的需求,通过爬虫来知晓上天的安排:
开搞!
1.网站分析
第一步呢,咋们先打开这个网站:https://www.horoscope.com/us/index.aspx
大家就能看到这个页面了
我们今天呢,就先做一个通过星座来得知三天的运势的小玩意,
这里有十二个星座,我点了第一个和第二个进去,也就是白羊座和金牛座:
就会发现一个规律
通过观察网址的链接,我这张丑脸泛起了灿烂的笑容。
也就是说,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.获取内容
从图片我们可以得到我们所需的内容,这个是很基础的爬虫了,没有反爬,我们直接上代码了。
3.代码
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"
return sign
4.实操
怎么样?很有趣吧,当然网站有很多的用处,等以后我会继续更新,实现更多的好玩的功能。
5.代码整合
from bs4 import BeautifulSoup
import requests
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"
return sign
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
if __name__ == "__main__":
print("Daily Horoscope. \n")
print(
"输入你的星座号码:\n",
"1. Aries\n",
"2. Taurus\n",
"3. Gemini\n",
"4. Cancer\n",
"5. Leo\n",
"6. Virgo\n",
"7. Libra\n",
"8. Scorpio\n",
"9. Sagittarius\n",
"10. Capricorn\n",
"11. Aquarius\n",
"12. Pisces\n",
"\n或者,如果您不确定自己的星座,请输入'calculate'",
)
zodiac_sign = input("number> ")
if zodiac_sign != "calculate":
print("choose some day:\n", "yesterday\n", "today\n", "tomorrow\n")
day = input("enter the day> ")
horoscope_text = horoscope(zodiac_sign, day)
print(horoscope_text)
else:
print("\n好的,别担心。很快你就会通过这个小测验")
print("\n恭喜!你绝对是", check_sign())
来源:https://akastan.blog.csdn.net/article/details/119851554
标签:python,爬虫,算命
0
投稿
猜你喜欢
mysql 卡死 大部分线程长时间处于sending data的状态
2024-01-23 06:43:48
Python 游戏大作炫酷机甲闯关游戏爆肝数千行代码实现案例进阶
2023-06-03 17:37:26
Jupyter notebook运行后打不开网页的问题解决
2021-07-10 01:35:30
举例讲解Python中的Null模式与桥接模式编程
2021-10-05 09:06:34
如何愉快地迁移到 Python 3
2021-06-25 19:44:17
Python光学仿真之对光的干涉理解学习
2021-05-24 04:47:52
TensorFlow实现模型评估
2023-10-15 22:36:51
sqlserver通用的删除服务器上的所有相同后缀的临时表
2012-06-06 20:07:34
Python实例方法、类方法、静态方法区别详解
2021-05-31 21:35:58
python实现雪花飘落效果实例讲解
2022-08-29 07:31:55
tensorflow 1.0用CNN进行图像分类
2022-08-17 17:32:29
详解Python 装饰器执行顺序迷思
2023-12-30 23:55:23
Python字典高级用法深入分析讲解
2022-10-31 06:43:20
php绘制圆形的方法
2023-10-29 17:31:52
linux系统使用python获取cpu信息脚本分享
2021-10-18 17:45:30
CentOS7下python3.7.0安装教程
2023-09-26 21:42:56
asp(JavaScript)自动判断网页编码并转换的代码
2011-03-03 11:19:00
php注册登录系统简化版
2024-04-30 08:48:24
python编写学生成绩管理系统的逻辑结构及功能实现
2021-08-23 18:32:21
python协程用法实例分析
2021-09-03 15:48:01