中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何用python獲取杭州天氣

發布時間:2022-01-13 09:33:23 來源:億速云 閱讀:190 作者:iii 欄目:大數據

本文小編為大家詳細介紹“如何用python獲取杭州天氣”,內容詳細,步驟清晰,細節處理妥當,希望這篇“如何用python獲取杭州天氣”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

【目的】

寫個小程序,可以直接獲取杭州天氣

【示例代碼】

# coding=utf-8
# @Auther : "鵬哥賊優秀"
# @Date : 2019/7/28
# @Software : PyCharm
import random
from bs4 import BeautifulSoup
import requests
import time
import sys

reload(sys)
sys.setdefaultencoding('utf-8')
def getheaders():
   user_list = [
       "Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16",
       "Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14",
       "Mozilla/5.0 (Windows NT 6.0; rv:2.0) Gecko/20100101 Firefox/4.0 Opera 12.14",
       "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0) Opera 12.14",
       "Opera/12.80 (Windows NT 5.1; U; en) Presto/2.10.289 Version/12.02",
       "Opera/9.80 (Windows NT 6.1; U; es-ES) Presto/2.9.181 Version/12.00",
       "Opera/9.80 (Windows NT 5.1; U; zh-sg) Presto/2.9.181 Version/12.00",
       "Opera/12.0(Windows NT 5.2;U;en)Presto/22.9.168 Version/12.00",
       "Opera/12.0(Windows NT 5.1;U;en)Presto/22.9.168 Version/12.00",
       "Mozilla/5.0 (Windows NT 5.1) Gecko/20100101 Firefox/14.0 Opera/12.0",
       "Opera/9.80 (Windows NT 6.1; WOW64; U; pt) Presto/2.10.229 Version/11.62",
       "Opera/9.80 (Windows NT 6.0; U; pl) Presto/2.10.229 Version/11.62",
       "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
       "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; de) Presto/2.9.168 Version/11.52",
       "Opera/9.80 (Windows NT 5.1; U; en) Presto/2.9.168 Version/11.51",
       "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; de) Opera 11.51",
       "Opera/9.80 (X11; Linux x86_64; U; fr) Presto/2.9.168 Version/11.50",
       "Opera/9.80 (X11; Linux i686; U; hu) Presto/2.9.168 Version/11.50",
       "Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11",
       "Opera/9.80 (X11; Linux i686; U; es-ES) Presto/2.8.131 Version/11.11",
       "Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/5.0 Opera 11.11",
       "Opera/9.80 (X11; Linux x86_64; U; bg) Presto/2.8.131 Version/11.10",
       "Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10",
       "Opera/9.80 (Windows NT 5.1; U; zh-tw) Presto/2.8.131 Version/11.10",
       "Opera/9.80 (Windows NT 6.1; Opera Tablet/15165; U; en) Presto/2.8.149 Version/11.1",
       "Opera/9.80 (X11; Linux x86_64; U; Ubuntu/10.10 (maverick); pl) Presto/2.7.62 Version/11.01",
       "Opera/9.80 (X11; Linux i686; U; ja) Presto/2.7.62 Version/11.01",
       "Opera/9.80 (X11; Linux i686; U; fr) Presto/2.7.62 Version/11.01",
       "Opera/9.80 (Windows NT 6.1; U; zh-tw) Presto/2.7.62 Version/11.01",
       "Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.7.62 Version/11.01",
       "Opera/9.80 (Windows NT 6.1; U; sv) Presto/2.7.62 Version/11.01",
       "Opera/9.80 (Windows NT 6.1; U; en-US) Presto/2.7.62 Version/11.01",
       "Opera/9.80 (Windows NT 6.1; U; cs) Presto/2.7.62 Version/11.01",
       "Opera/9.80 (Windows NT 6.0; U; pl) Presto/2.7.62 Version/11.01",
       "Opera/9.80 (Windows NT 5.2; U; ru) Presto/2.7.62 Version/11.01",
       "Opera/9.80 (Windows NT 5.1; U;) Presto/2.7.62 Version/11.01",
       "Opera/9.80 (Windows NT 5.1; U; cs) Presto/2.7.62 Version/11.01",
       "Mozilla/5.0 (Windows NT 6.1; U; nl; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 Opera 11.01",
       "Mozilla/5.0 (Windows NT 6.1; U; de; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 Opera 11.01",
       "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; de) Opera 11.01",
       "Opera/9.80 (X11; Linux x86_64; U; pl) Presto/2.7.62 Version/11.00",
       "Opera/9.80 (X11; Linux i686; U; it) Presto/2.7.62 Version/11.00",
       "Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.6.37 Version/11.00",
       "Opera/9.80 (Windows NT 6.1; U; pl) Presto/2.7.62 Version/11.00",
       "Opera/9.80 (Windows NT 6.1; U; ko) Presto/2.7.62 Version/11.00",
       "Opera/9.80 (Windows NT 6.1; U; fi) Presto/2.7.62 Version/11.00",
       "Opera/9.80 (Windows NT 6.1; U; en-GB) Presto/2.7.62 Version/11.00",
       "Opera/9.80 (Windows NT 6.1 x64; U; en) Presto/2.7.62 Version/11.00",
       "Opera/9.80 (Windows NT 6.0; U; en) Presto/2.7.39 Version/11.00"
   ]
   user_agent = random.choice(user_list)
   headers = {'User-Agent': user_agent}
   return headers


def weather():
   url = "http://www.weather.com.cn/weather/101210101.shtml"
   headers = getheaders()
   r = requests.get(url, headers=headers)
   r.encoding = 'utf-8'
   soup = BeautifulSoup(r.text, "lxml")
   lines = soup.find('li', class_="sky skyid lv2 on")
   date = lines.h2.get_text()
   weather = lines.p.string
   low_temp = lines.find('p', class_="tem").i.get_text()
   if not lines.find('p', class_="tem").span:
       high_temp = "未知"
   else:
       high_temp = lines.find('p', class_="tem").span.get_text()
   temp = high_temp + '-' + low_temp
   wind = lines.find('span', class_="S").get('title')
   windnumber = lines.find('p', class_="win").i.get_text()
   result = {
       "日期": date,
       "天氣": weather,
       "溫度": temp,
       "風向": wind,
       "風級": windnumber
   }
   print(u"日期:{0} \n天氣:{1} \n溫度:{2} \n風向:{3} \n風級:{4}").format(date, weather, temp, wind, windnumber)
   return result

if __name__ == "__main__":
   weather()
   time.sleep(5)

    

【效果如下】:

如何用python獲取杭州天氣

讀到這里,這篇“如何用python獲取杭州天氣”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

芜湖县| 江阴市| 金塔县| 四子王旗| 黄山市| 定边县| 崇左市| 巫山县| 鞍山市| 镇雄县| 获嘉县| 中方县| 英德市| 抚顺县| 桐柏县| 大理市| 新河县| 沁水县| 武穴市| 宿州市| 浮梁县| 阿拉善右旗| 昌邑市| 囊谦县| 东莞市| 慈溪市| 固安县| 巫山县| 莱芜市| 深泽县| 拉孜县| 平陆县| 崇州市| 洛浦县| 武陟县| 托克逊县| 河池市| 资讯| 大邑县| 高陵县| 乳源|