A simple Python weather forecast

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.