一小伙使用 python爬虫来算命?

前言

相信在日常生活中,平常大家聚在一起总会聊聊天,特别是女生(有冒犯到doge)非常喜欢聊星座,这个男生什么星座呀,那个男生什么星座呀…今天我就来满足各位的需求,通过爬虫来知晓上天的安排:

开搞!

1.网站分析

因版权原因,网站的地址大家可以私信我或者加我文章结尾的qq,完整的教程群里有,需要的自提,当然遇到问题也可以请教哦。

2.获取内容

我们今天呢,就先做一个通过星座来得知三天的运势的小玩意,

这里有十二个星座,我点了第一个和第二个进去,也就是白羊座和金牛座:

就会发现一个规律

请添加图片描述
在这里插入图片描述

通过观察网址的链接,我这张丑脸泛起了灿烂的笑容。

也就是说,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

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.代码

需要完整代码和教程可以加qq群自提:785318982
群内有大佬,如果遇到学习问题,可以在里面问。


如果感觉还不错的话点赞,关注支持一下吧,谢谢啦!

  • 522
    点赞
  • 1800
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 132
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 132
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

弈鸣coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值