• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python爬虫竟然被小伙用来算命

python 搞代码 4年前 (2022-01-07) 32次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了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.获取内容

从图片我们可以得到我们所需的内容,这个是来源gaodai#ma#com搞*!代#%^码$网很基础的爬虫了,没有反爬,我们直接上代码了。

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) = 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) <= 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) = 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)  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()) 

到此这篇关于python爬虫竟然被小伙用来算命的文章就介绍到这了,更多相关python爬虫算命内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网

以上就是python爬虫竟然被小伙用来算命的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python爬虫竟然被小伙用来算命

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址