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

yizhihongxing

1.获取内容

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

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

就会发现一个规律
 一小伙使用 python爬虫来算命?

 

 

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

 

 

 

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

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

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

 

 

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

 

 

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

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:一小伙使用 python爬虫来算命? - Python技术站

(0)
上一篇 2023年4月2日 下午4:52
下一篇 2023年4月2日 下午4:53

相关文章

  • 什么是 Python?Python 基础编程入门指南

    Python是当今最流行的编程语言之一。Python以其简单的语法和多功能性而闻名,既易于学习又可用于高级应用程序。可以使用Python的领域也非常广泛,人工智能、机器学习、Web 开发…基本上绝大多数热门的域都能看到Python的身影。   今天,我们将深入了解 Python 是什么,它经常用于什么,以及如何从今天开始学习 Python。   Python…

    2023年4月2日
    00
  • 【pygame游戏】用Python实现一个蔡徐坤大战篮球的小游戏,可还行?【附源码】

    准备工作开发环境 Python版本:3.7.8 相关模块:requests模块;tqdm模块;pyfreeproxy模块;pyechats模块;以及一些python自带的模块。 效果预览开始界面   游戏规则 wasd 控制人物的移动,空格启动律师函炸毁全部篮球。            代码实现 导入模块 import pygame import sys i…

    2023年4月2日
    00
  • python进行敏感性分析(SALib库)

    什么是敏感性分析  敏感性分析(sensitivity analysis)是指从定量分析的角度研究有关因素发生某种变化对某一个或一组关键指标影响程度的一种不确定分析技术。每个输入的灵敏度用某个数值表示即敏感性指数(sensitivity index) 敏感性指数包括以下几种: 一阶指数:度量单个模型输入对输出方差的贡献 二阶指数:度量两个模型输入的相互作用对…

    2023年4月2日
    00
  • Python+matplotlib实现折线图的美化

    1. 导入包 import pandas as pd import matplotlib.pyplot as plt import matplotlib.ticker as ticker import matplotlib.gridspec as gridspec 2. 获得数据 file_id = ‘1yM_F93NY4QkxjlKL3GzdcCQEnBi…

    2023年3月31日
    00
  • 这个Python读取文件的方法,堪称天花板级别…

    1、方法介绍基本用法 先来看一下fileinput的基本功能: fileinput.filename():返回当前被读取的文件名。—>在第一行被读取之前,返回 None。 fileinput.fileno():返回以整数表示的当前文件“文件描述符”。—>当未打开文件时(处在第一行和文件之间),返回 -1。 fileinput.lineno():返…

    2023年4月2日
    00
  • python烟花代码

    python烟花代码   如下 # -*- coding: utf-8 -*- import math, random,time import threading import tkinter as tk import re #import uuid Fireworks=[] maxFireworks=8 height,width=600,600 class…

    2023年4月2日
    00
  • Python基础语法:函数

    1 函数定义   1.1 函数概述 在程序设计中,函数的使用可以提升代码的复用率和可维护性。 提升代码的复用率: 程序设计中,一些代码的功能是相同的,操作是一样的,只不过针对的数据不一样。此种情况下,可以将这种功能写成一个函数模块,要使用此功能时只需调用这个函数模块就可以了。提升代码的可维护性: 使用函数后,实现了代码的复用,某个功能需要核查或修改时,只需要…

    2023年4月2日
    00
  • Python中的列表条件求和方法

    列表条件求和方法   list_data=[ [1.0, ‘配件’, ‘522422’, ‘铝扣板用纽扣’, ‘金色’, ”, 72.0, ‘PC’, ”], [2.0, ‘配件’, ‘500031’, ‘十字槽沉头自钻自攻螺钉4.2*45’, ‘原色’, ”, 72.0, ‘PC’, ”], [1.0, ‘配件’, ‘522422’, ‘铝扣板用纽…

    Python开发 2023年4月2日
    00
合作推广
合作推广
分享本页
返回顶部