Python obtains weather data, does visual analysis, and completes necessary projects

foreword

(。・∀・)ノ゙Hi everyone

Let me chat with you again~

The weather in Changsha in the past few days has been sunny for two days and rainy for one day. My brother and his teacher asked them to crawl through the weather data and do a visual analysis by the way.

He's fine, he doesn't listen in class, and now he comes over to ask me for help, what else can I do, then help him

insert image description here

By the way, let me share with you how Python collects weather data and visualizes it (this can also be done as a final project or a class assignment)

knowledge points

  • Dynamic data capture
  • requestsSend requests
  • Structured + unstructured data analysis

development environment

  • Python 3.8 to run the code
  • pycharm 2022.3.2 Auxiliary Knock Code Professional Edition
  • requests send request pip install requests
  • parsel parse data pip install parsel

Reptile case implementation

1. Thought analysis

Find data sources Static data? Dynamic data?

Network packet capture analysis

2. Code implementation

  1. send request
  2. retrieve data
  3. Analytical data
  4. save data

Complete code [Click to receive the business card at the end of the article]

import requests
import parsel
import csv

In addition to the code, other information is required [click on the business card at the end of the article]

insert image description here

insert image description here

f = open('天气数据.csv', mode='a', newline='', encoding='utf-8')
csv_writer = csv.writer(f)
csv_writer.writerow(['日期','最高温度','最低温度','天气', '风向','城市'])
areaList = [54511, 58362, 59287, 59493]
for areaId in areaList:
    if areaId == 54511:
        area = "北京"
    elif areaId == 58362:
        area = "上海"
    elif areaId == 59287:
        area = "广州"
    else:
        area = "深圳"
        690643772 ### 源码领取
    for year in range(2011, 2023):
        for month in range(1, 13):
            url = f'https://'
  1. send request
response = requests.get(url)
  1. retrieve data

.text: Get text content.content
: Binary data image/audio/video.json
(): Get json data string {"":"", "":"", "":""}

 json_data = response.json()
  1. Analytical data

What data is parsed?
Structured data: json data dictionary value extraction content
Unstructured data: web page source code css/xpath/re bs4/lxml/parsel/re…

html_data = json_data['data']
# tr
select = parsel.Selector(html_data)
trs = select.css('tr')[1:]
for tr in trs:690643772 ### 源码领取
   # .get(): 获取单个标签
   # .getall(): 获取所有标签
   td = tr.css('td::text').getall()
   td.append(area)
   csv_writer.writerow(td)
   print(td)

at last

Today's case sharing ends here

Leave a message in the iron juice comment area with questions about the article, or click on the business card at the end of the article to communicate and learn

insert image description here

Guess you like

Origin blog.csdn.net/yxczsz/article/details/129326383