[interesting case] Python command line implementation - check the national 7-day weather

Why climb the weather? 1. You can practice. 2. After using the itchat library to realize the function of automatic reply, integrate the function of weather check, and realize the function of wechat self-service weather check!

First of all, it's a similar pattern. Let's see if we can grab a general API directly on the official website (XHR). Then directly use API to query OK? Search Baidu for the keyword [weather] or [Nanjing Weather] and the corresponding webpage will pop up: Click http://www.weather.com.cn/weather/101190101.shtml. You can see the weather conditions of the corresponding city in the next week:

In another city, Shanghai, we found that the browser address changed to: http://www.weather.com.cn/weather/101020100.shtml. The original 101020100 number corresponds to the code of the corresponding city. Let's analyze the XHR request on the next page to see if it is possible to grab the package directly?

After Google browser - check - Networt-XHR - refresh, no XHR request is found. It seems that the weather content and city code we need may be included in the page and presented after JS and server processing.... OK, the attempt failed!

Look at the JS request again. It's too much to check one by one. Fortunately, someone on the Internet has already recorded the city codes corresponding to all cities. I copied it and saved it to my local mysql. The data is on Baidu cloud. You can download what you need, and execute SQL to directly build the SQL table and data together. https://pan.baidu.com/s/1kXaN2Aj The password is: 8y6n.

Now that the preparations are finished, the idea is clear. There are cities and codes in China. We can check the weather of a city, just input the city, and get the corresponding city code from mysql, such as 101020100, and then construct the corresponding url: http://www.weather.com.cn/weather/101190101.shtml can view the weather of the corresponding city for 7 days. However, the page does not have XHR and directly available json data, so we can only do it ourselves - analyze the content of the page, and write the regular expression / beautiful soup / XPath to extract the page information. The specific content will not be described here. Please refer to the code for details:

What I don't know in the learning process can add to me
python learning resource  qun,855408893
//There are good learning video tutorials, development tools and e-books in the group.
//Share with you the current talent needs of python enterprises and how to learn python from scratch, and what to learn 

import re 
import pymysql 
import requests 
from bs4 import BeautifulSoup 

class SearchWeather(): 
    def __init__(self): 
        self.HEADERS ={ 
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 ''(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'} 
        self.CONNECTION = pymysql.connect(host='localhost',user='root',password='xxx',db='xxx',charset='utf8',cursorclass=pymysql.cursors.DictCursor) 

    def getcityCode(self,cityName): 
        SQL = "SELECT cityCode FROM cityWeather WHERE cityName='%s'" % cityName 
            with self.CONNECTION.cursor() as cursor: 
                result = cursor.fetchone() 
                return result['cityCode'] 
        except Exception as e: 

    def getWeather(self,cityCode,cityname): 
        url = 'http://www.weather.com.cn/weather/%s.shtml' % cityCode 
        html = requests.get(url,headers = self.HEADERS) 
        weather = "date      weather    [temperature]    Wind direction and wind force\n" 
        for item in soup.find("div", {'id': '7d'}).find('ul').find_all('li'): 
            date,detail = item.find('h1').string, item.find_all('p') 
            title = detail[0].string 
            templow = detail[1].find("i").string 
            temphigh = detail[1].find('span').string if detail[1].find('span')  else '' 
            wind,direction = detail[2].find('span')['title'], detail[2].find('i').string 
            if temphigh=='': 
                weather += 'Hello[%s]Today's Day:[%s],Temperature:[%s],%s: [%s]\n' % (cityname,title,templow,wind,direction) 
                weather += (date + title + "[" + templow +  "~"+temphigh +'°C]' + wind + direction + "\n") 
        return weather 

    def main(self,city): 
        cityCode = self.getcityCode(city) 
        detail = self.getWeather(cityCode,city) 
        print (detail) 

if __name__ == "__main__": 
    weather = SearchWeather() 
    weather.main(city=input('Please enter the city name:')) 

The code runs as follows:

Keywords: Programming SQL Python MySQL Google

Added by expertis on Tue, 21 Apr 2020 10:55:26 +0300