目錄
- 前言
- 1.網(wǎng)站分析
- 2.獲取內(nèi)容
- 3.代碼
- 4.實(shí)操
- 5.代碼整合
前言
相信在日常生活中,平常大家聚在一起總會(huì)聊聊天,特別是女生(有冒犯到doge)非常喜歡聊星座,這個(gè)男生什么星座呀,那個(gè)男生什么星座呀…今天我就來(lái)滿足各位的需求,通過(guò)爬蟲(chóng)來(lái)知曉上天的安排:
開(kāi)搞!
1.網(wǎng)站分析
第一步呢,咋們先打開(kāi)這個(gè)網(wǎng)站:https://www.horoscope.com/us/index.aspx
大家就能看到這個(gè)頁(yè)面了
我們今天呢,就先做一個(gè)通過(guò)星座來(lái)得知三天的運(yùn)勢(shì)的小玩意,
這里有十二個(gè)星座,我點(diǎn)了第一個(gè)和第二個(gè)進(jìn)去,也就是白羊座和金牛座:
就會(huì)發(fā)現(xiàn)一個(gè)規(guī)律
通過(guò)觀察網(wǎng)址的鏈接,我這張丑臉?lè)浩鹆藸N爛的笑容。
也就是說(shuō),https://www.horoscope.com/us/horoscopes/general/是每個(gè)星座都共有的一段網(wǎng)址,
horoscope-general-daily-today.aspx?sign=1
我們只需改變today和sign={}對(duì)應(yīng)的值就可以獲取到每個(gè)星座對(duì)應(yīng)的網(wǎng)址了
https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-today.aspx?sign=1
我們?cè)俅蜷_(kāi)金牛座的昨天的運(yùn)勢(shì),發(fā)現(xiàn)daily-后面變成了tomorrow
我們這里只獲取三天的,也就是昨天,今天,明天,我們只需在控制臺(tái)輸入需要查詢的日期就行了。
2.獲取內(nèi)容
從圖片我們可以得到我們所需的內(nèi)容,這個(gè)是很基礎(chǔ)的爬蟲(chóng)了,沒(méi)有反爬,我們直接上代碼了。
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#返回得到的內(nèi)容——來(lái)自上天的指示
如果有小伙伴不知道自己的星座怎么辦呢,所以我們就還需要一個(gè)函數(shù)去查詢星座:
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.實(shí)操
怎么樣?很有趣吧,當(dāng)然網(wǎng)站有很多的用處,等以后我會(huì)繼續(xù)更新,實(shí)現(xiàn)更多的好玩的功能。
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) = 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
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
if __name__ == "__main__":
print("Daily Horoscope. \n")
print(
"輸入你的星座號(hào)碼:\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或者,如果您不確定自己的星座,請(qǐng)輸入'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好的,別擔(dān)心。很快你就會(huì)通過(guò)這個(gè)小測(cè)驗(yàn)")
print("\n恭喜!你絕對(duì)是", check_sign())
到此這篇關(guān)于python爬蟲(chóng)竟然被小伙用來(lái)算命的文章就介紹到這了,更多相關(guān)python爬蟲(chóng)算命內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- js日期、星座的級(jí)聯(lián)顯示代碼
- javascript計(jì)算星座屬相(十二生肖屬相)示例代碼
- php根據(jù)日期或時(shí)間戳獲取星座信息和生肖等信息
- php根據(jù)日期顯示所在星座的方法
- 根據(jù)出生日期自動(dòng)取得星座的js代碼
- python實(shí)現(xiàn)根據(jù)月份和日期得到星座的方法
- jQuery實(shí)現(xiàn)根據(jù)生日計(jì)算年齡 星座 生肖
- 使用php從身份證號(hào)中獲取一系列線索(星座、生肖、生日等)