网站建设资讯

NEWS

网站建设资讯

如何用python获取杭州天气

本文小编为大家详细介绍“如何用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获取杭州天气”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注创新互联行业资讯频道。


分享题目:如何用python获取杭州天气
路径分享:http://cdweb.net/article/jjcjhe.html