首页 > 编程语言 >一小伙使用 python爬虫来算命?

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

时间:2022-09-26 22:46:01浏览次数:58  
标签:小伙 python sign 爬虫 day int birth month your

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"
PythonQUN:748,989,764 
 return sign


4.实操

 

 

 

 

 

怎么样?很有趣吧,当然网站有很多的用处,等以后我会继续更新,实现更多的好玩的功能。

标签:小伙,python,sign,爬虫,day,int,birth,month,your
From: https://www.cnblogs.com/liuliumei/p/16732820.html

相关文章

  • 【Python】网络爬虫
    本章主要讲的是基于Python语言的数据采集,该功能要讲起来可以单独作为一门课程来学习,因为这是一门很重要的课程,一般运用在大数据处理和人工智能上,该应用提供大量的数据。1.......
  • python 集合
    集合(set)是一个无序、可变、不允许数据重复的容器。1.定义s={11,22,33}无序,无法通过索引取值可变,可以添加和删除元素不允许数据重复用途:如果有一个数据类型......
  • python2和python3区别
    区别1:python2中需要用户自己制定数据类型,写什么类型就是什么数据类型,写字符串要加双引号。python3中不管什么数据类型都会转成字符串。python中的raw_input与python3......
  • python学习day04
    上周内容回顾PEP8规范1.井号后跟注释文字时,井号和前面的代码空两格,和后面的注释文字空一格。2.井号单独起一行后跟注释文字时,和后面的注释文字空一格。3.列表、......
  • python5
    python数据类型数据类型-布尔值(bool)1.判断失误的对错是否可行只用于流程控制中2.只有两种状态:True对的False错的3.python中所有数值自带布尔值......
  • python小白入门学习day04
    python小白入门学习day04目录§一、周末内容回顾1、PEP8规范2、python注释语法3、变量与常量4、数据类型§二、今日内容详细1、作业详解2、基本数据类型之布尔值bool2、基......
  • python
    python数据类型基础数据类型之布尔值bool+布尔值bool用来判断事物的对错,是不是可行。主要用于流程控制中只有两种形态1.True对的,真的,可行的2.False错的,假的,......
  • Python-函数-算术函数
    #python-函数-算术函数#(1)加减乘除#加法add(),减法subtract(),乘法multiply(),除法divide()#作用:数组间的加减乘除importnumpyasnpx=np.arange(9,dtype=......
  • python基本数据类型二
    python基本数据类型二1.布尔值bool描述用于判断事物的对错,是否可行,主要用于流程控制状态只有两种状态True和falseTrue对的,真的,可行的False错的,假的,不可行......
  • python中组合数据的操作
    2022-09-26组合数据类型:列表   字典   集合   元组拷贝:   deep(深拷贝)   shallow(浅拷贝)  区别:例如,文件中有一个指针指向另一块存储空间......