python爬虫竟然被小伙用来算命
作者:弈鸣敲代码 发布时间:2023-08-28 17:35:20
标签:python,爬虫,算命
前言
相信在日常生活中,平常大家聚在一起总会聊聊天,特别是女生(有冒犯到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来处理数据(海量的数据),我们都是把这个数据转换为Python的对象的,比如最为常见的字典。比如
- 当我们进行数据分析时,有时候需要对数值型数据进行离散化,将其划分为不同的标签或类别。这样做可以方便我们进行统计和分析,并帮助我们更好地理解数
- 1. 安装 Sublime Text 3虽然现在的 Sublime 3 还处于 beta 阶段, 但已经非常稳定了, 而且速度比 Subli
- 本文实例讲述了JS+HTML5 canvas绘制验证码。分享给大家供大家参考,具体如下:css样式:<style>body{ &
- 现象:在IE下,用JS修改p标签的innerHTML时,出"未知的运行时错误(unknown runtime error)&quo
- 远程登陆SQLServer (2014)数据库,供大家参考,具体内容如下两台电脑,同一个局域网内,IP同一网段配置:Computer1: W
- 脚本语言的第一行的目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它。写法:#!/usr/bin/python是告诉操作系统执
- 1.简介Entity Framework Core可通过数据库提供给应用程序的插件访问许多不同的数据库。我们可以通过使用Entity Fra
- 如下所示:def read_data(file_name): ''' file_name:文件地址 '
- 由于要学习搭建服务器和数据库,所以最近开始自学sql语言了,至于写数据库就用比较基础的Mysql数据库了,虽然Mysql已经被互联网公司所淘
- 设置表名为中文1.设置Models.py文件class Post(models.Model): name = models.CharFiel
- 在上篇文章给大家介绍了BootstrapTable与KnockoutJS相结合实现增删改查功能【一】,介绍了下knockout.js的一些基
- 关于模型保存的一点心得saver = tf.train.Saver(max_to_keep=3)在定义 saver 的时候一般会定义最多保存
- 以前在一个图书类网站看到这样一个功能:客户可以按条件搜索书目的信息,服务器会将符合条件的信息筛选出来保存为一个Excel文件供客户下载。今天
- 本文实例讲述了python sqlite的Row对象操作。分享给大家供大家参考,具体如下:一 代码import sqlite3conn=sq
- mysql找不到my.ini文件问题描述刚刚在修改mysql默认配置的时候,发现找不到my.ini文件。按照其他搬运工的说法,打开隐藏的文件
- 为了使一个MySQL系统安全,强烈要求你考虑下列建议……当你连接一个MySQL服务器时,你通常应
- 关于MySQL的事务隔离级别,相信很多读者都不陌生,网商有很多种相关的文章,很多人对于各种隔离级别,以及不同的级别可以解决的一些读现象都是如
- 引入Gridgrid=Grid() # 可以分别调整上下左右的位置,可以是百分比,也可以是具体像素,如pos_top="50px&
- 如果在INSERT语句末尾指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY K