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

打印 上一主题 下一主题

主题 823|帖子 823|积分 2484

1.网站分析
因版权原因,网站的地址大家可以私信我或者加我文章结尾的qq,完整的教程群里有,需要的自提,当然遇到问题也可以请教哦。
2.获取内容
我们今天呢,就先做一个通过星座来得知三天的运势的小玩意,
这里有十二个星座,我点了第一个和第二个进去,也就是白羊座和金牛座:
就会发现一个规律
 
 
 
 
 
 
 
通过观察网址的链接,我这张丑脸泛起了灿烂的笑容。
也就是说,https://www.horoscope.com/us/horoscopes/general/是每个星座都共有的一段网址,
horoscope-general-daily-today.aspx?sign=1
我们只需改变today和sign={}对应的值就可以获取到每个星座对应的网址了
 
  1. https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-today.aspx?sign=1
复制代码
我们再打开金牛座的昨天的运势,发现daily-后面变成了tomorrow
 3.代码
  1. from bs4 import BeautifulSoup
  2. import requests
  3. def horoscope(zodiac_sign: int, day: str) -> str:
  4.     url = (
  5.         "https://www.horoscope.com/us/horoscopes/general/"
  6.         f"horoscope-general-daily-{day}.aspx?sign={zodiac_sign}"
  7.     )#获取需要查询的星座的链接
  8.     soup = BeautifulSoup(requests.get(url).content, "html.parser")
  9.     return soup.find("div", class_="main-horoscope").p.text#返回得到的内容——来自上天的指示
复制代码
如果有小伙伴不知道自己的星座怎么办呢,所以我们就还需要一个函数去查询星座:
[code]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) = 20) or (        int(your_birth_month) == 2 and int(your_birth_day) = 18) or (        int(your_birth_month) == 3 and int(your_birth_day) = 20) or (        int(your_birth_month) == 4 and int(your_birth_day) = 20) or (        int(your_birth_month) == 5 and int(your_birth_day) = 21) or (        int(your_birth_month) == 6 and int(your_birth_day) = 21) or (        int(your_birth_month) == 7 and int(your_birth_day) = 23) or (        int(your_birth_month) == 8 and int(your_birth_day) = 23) or (        int(your_birth_month) == 9 and int(your_birth_day) = 23) or (        int(your_birth_month) == 10 and int(your_birth_day) = 23) or (        int(your_birth_month) == 11 and int(your_birth_day) = 22) or (        int(your_birth_month) == 12 and int(your_birth_day)

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大连全瓷种植牙齿制作中心

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表