The light Raspberry Pi has always been the love of everyone, in the development of some small things so that we have a sense of accomplishment, and under Linux, Python can make troublesome operation become simple, and the Raspberry Pi power is very low, combined, perfect!
1, directly into the topic, generally in the Linux or Raspberry Pi Raspberry system will be automatically installed Python, and we also need a module under Python called requests, he needs PIP installation, so we first want to install PIP
Curl-o https://bootstrap.pypa.io/get-pip.pyget-pip.py
2, install requests after installation.
PIP Install requests
It's ready to use after installation (I can't)
3, installed after the start of our brains, first we want to achieve the function is the weather forecast, from where to get the weather this is a problem, here can use http://www.weather.com.cn/data/sk/101051301.html this interface, 101051301 is the ID of the city, can be viewed to http://www.weather.com.cn/, replaced after the browser opens
(those words that don't understand are coding problems.)
The next thing is how to get, understand the English of all see the Cityid is the city id,temp is the temperature, SD is humidity, we get these three information, other similar
4, writing code
VI tianqi.py
#!/usr/bin/python #这里是python的目录import requests #引用requests模块r=requests.Get('http://www.weather.com.cn/data/sk/101051301.html') #获取r. Encoding='Utf-8'#编码print"City :"+ R.json () ['Weatherinfo'][' City'],"\nwendu:"+r.json () ['Weatherinfo']['Temp'],"\nshidu:"+r.json () ['Weatherinfo']['SD'] #获取我们想要的信息
5,:wq save, give tianqi.py run permission, chmod u+x tianqi.py
6, run./tianqi.py Output results:
Complete
Summary: In the example of using Python to get the weather forecast, our main task is to find the appropriate API to solve the character set encoding problem. When these problems are solved, get the weather forecast directly using the requests library. Write the program is not afraid you will not programming language, fear is that you do not have a program of thinking, how to do, how to complete the work of the program, it is necessary to exercise slowly!
A simple Python weather forecast