| 日期 | 開始時間 | 結束時間 | 中斷時間 | 凈時間 | 活動 | 活動詳解 |
| 3.10 | 3:40 | 5:30 | 30 | 70 | 查看爬取數據的資料 | 查看python |
| 3.10 | 7:00 | 8:30 | 0 | 90 | 安裝軟件 | 學習python的基礎知識 |
| 3.11 | 10:00 | 11:10 | 20 | 50 | 學習python爬取數據 | |
| 3.11 | 2:30 | 5:00 | 25 | 125 | 編寫代碼 | 連數據庫,爬取數據 |
| 3.12 | 2:30 | 4:00 | 20 | 70 | 改代碼 |
爬取數據:

python代碼:
#-*- coding=utf-8 -*-
import pymysql
import re
import json
from pip._vendor import requests
def create():
# 連接數據庫
db = pymysql.connect("localhost", "root", "123", "payiqing",charset='utf8')
cursor = db.cursor()
cursor.execute("DROP TABLE IF EXISTS info")
sql = """CREATE TABLE info(
Id INT PRIMARY KEY AUTO_INCREMENT,
Date varCHAR(255),
Province varchar(255),
City varchar(255),
Confirmed_num varchar(255),
Yisi_num varchar(255),
Cured_num varchar(255),
Dead_num varchar(255),
Code varchar(255))"""
#創建數據庫變量
cursor.execute(sql)
db.close()
def insert(value):
db = pymysql.connect("localhost", "root", "123", "payiqing",charset='utf8')
cursor = db.cursor()
sql = "INSERT INTO info(Date,Province,City,Confirmed_num,Yisi_num,Cured_num,Dead_num,Code) VALUES ( %s,%s,%s,%s,%s,%s,%s,%s)"
try:
cursor.execute(sql, value)
db.commit()
print('插入數據成功')
except:
db.rollback()
print("插入數據失敗")
db.close()
# 創建表
create() # 創建表
url='https://raw.githubusercontent.com/BlankerL/DXY-2019-nCoV-Data/master/json/DXYArea.json'
response = requests.get(url)
# 將響應信息進行json格式化
versionInfo = response.text
# print(versionInfo)#打印爬取到的數據
# print("------------------------")#重要數據分割線↓
#一個從文件加載,一個從內存加載#json.load(filename)#json.loads(string)
jsonData = json.loads(versionInfo)
#用於存儲數據的集合
dataSource = []
provinceShortNameList = []
confirmedCountList = []
curedCount = []
deadCountList = []
#遍歷對應的數據存入集合中
for k in range(len(jsonData['results'])):
if(jsonData['results'][k]['countryName'] == '中國'):
provinceShortName = jsonData['results'][k]['provinceName']
if("待明確地區" == provinceShortName):
continue;
for i in range(len(jsonData['results'][k]['cities'])):
confirmnum=jsonData['results'][k]['cities'][i]['confirmedCount']
yisi_num=jsonData['results'][k]['cities'][i]['suspectedCount']
cured_num=jsonData['results'][k]['cities'][i]['curedCount']
dead_num=jsonData['results'][k]['cities'][i]['deadCount']
code=jsonData['results'][k]['cities'][i]['locationId']
cityname=jsonData['results'][k]['cities'][i]['cityName']
date='2020-3-10'
insert((date,provinceShortName,cityname,confirmnum,yisi_num,cured_num,dead_num,code))
