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.獲取內容
從圖片我們可以得到我們所需的內容,這個是很基礎的爬蟲瞭,沒有反爬,我們直接上代碼瞭。
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" 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) <= 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( "輸入你的星座號碼:\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爬蟲算命內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Python爬蟲網頁元素定位術
- python自動生成證件號的方法示例
- Python使用Beautiful Soup實現解析網頁
- 基於pycharm的beautifulsoup4庫使用方法教程
- 使用Python爬取小姐姐圖片(beautifulsoup法)