Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python to get the weather of Hangzhou

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to use python to get the weather in Hangzhou". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use python to get the weather in Hangzhou" can help you solve your doubts.

[objective]

Write a Mini Program, you can get the weather of Hangzhou directly.

[sample code]

# coding=utf-8

# @ Auther: "Pengge thief is excellent"

# @ 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 th Utteren) Presto/22.9.168 Version/12.00"

"Opera/12.0 (Windows NT 5.1 th Utteren) 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 x8634; 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 x8634; 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 x8634; 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 x8634; 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.1x64; 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 ('paired, class_= "tem") .i.get_text ()

If not lines.find ('paired, class_= "tem") .span:

High_temp = "unknown"

Else:

High_temp = lines.find ('paired, class_= "tem") .span.get_text ()

Temp = high_temp +'-'+ low_temp

Wind = lines.find ('span', class_= "S"). Get (' title')

Windnumber = lines.find ('paired, class_= "win") .i.get_text ()

Result = {

Date: date

Weather: weather

Temperature: temp

Wind Direction: wind

Wind scale: windnumber

}

Print (u "date: {0}\ nWeather: {1}\ ntemperature: {2}\ nWind Direction: {3}\ nWind scale: {4}") .format (date, weather, temp, wind, windnumber)

Return result

If _ name__ = = "_ _ main__":

Weather ()

Time.sleep (5)

[effects are as follows]:

After reading this, the article "how to use python to get the weather in Hangzhou" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report