如何用python获取杭州天气

发布时间:2022-01-13 09:33:23 作者:iii
来源:亿速云 阅读:193

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

推荐阅读:
  1. Python3 XML如何获取雅虎天气
  2. 如使用JS获取HTML DOM元素

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:android如何实现固定时间段内的工作时间求和

下一篇:如何优化定时任务与feign超时的纠葛

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》