Home > database >  Pycharm out ah KeyError: first in '2020-10-10' how to solve
Pycharm out ah KeyError: first in '2020-10-10' how to solve

Time:10-11

The import pymysql
The import requests
The import time
The import json
Import a datetime

"' access to all data

0, new links, the cursorClose close
1, the request data, converted to JSON
2, the data parsing (data. ChinaDayAddList
Data. ChinaDayList)
Generate the dict
{' 2020-09-29 ': {" confirm ": 200}}
3, the data is stored
"'


# date standardization
Def get_date (date) :
The date='2020' + date
Date_str=time. Strptime (date, "% d % % y. m.")
Date_s=time. Strftime (' % Y - % m - % d ', date_str)
Return date_s


# database connection
Def get_conn () :
Conn=pymysql. Connect (host="localhost", user="root", "=" root ",
The database="data_view", the port=3306, unix_socket=None,
Charset='utf8)
Cursor=conn. Cursor ()
# to empty tables, starting from 1 order
Cursor. The execute (' TRUNCATE history_data ')
MIT ()
conn.comReturn the conn, cursor


# close the database connection
Def close_conn (conn, cursor) :
If the cursor:
Cursor. The close ()
If the conn:
Conn. Close ()


# 1, the request data, converted to JSON
Def get_qq_data () :
Url="https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list? Modules=chinaDayList chinaDayAddList, cityStatis nowConfirmStatis, provinceCompare '
Headers={
'the user-agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, likeGecko) Chrome/85.0.4183.102 Safari/537.36 '
}
Resp=requests. Get (url, headers)
Data=https://bbs.csdn.net/topics/json.loads (resp. Text)
Return the data (' data ')


# 2, the data parsing
Def get_histroy_data (data) :
# daily cases
# data. ChinaDayList
ChinaDayList=data [' chinaDayList]
History_dict={}
For day_item chinaDayList in:
# date standardization
The date=day_item [' date ']
Date_f=get_date (date)
# has confirmed
Confirm=day_item [' confirm ']
# print (confirm)
# accumulative total cure
At heal=day_item [' at heal]
# the cumulative death
Dead=day_item [' dead ']
# existing diagnosis
NowConfirm=day_item [' nowConfirm]
# asymptomatic infection
NoInfect=day_item [' noInfect]
# outside input
ImportedCase=day_item [' importedCase]
# print (date_f, confirm, at heal, nowConfirm noInfect, importedCase)
History_dict [date_f]={' confirm ': confirm,' at heal: at heal, 'dead' : dead, 'nowConfirm: nowConfirm,
'noInfect: noInfect,' importedCase: importedCase, 'confirm_add: 0,' heal_add: 0,
'dead_add: 0,' infect_add: 0, 'importedCase_add: 0}
# print (history_dict)

# daily new cases
# data. ChinaDayAddList
ChinaDayAddList=data [' chinaDayAddList]
For add_item chinaDayAddList in:
# print (add_item)
# date standardization
The date=add_item [' date ']
Date_add=get_date (date)
# new diagnosis
Confirm_add=add_item [' confirm ']
New cure #
Heal_add=add_item [' at heal]
# new death
Dead_add=add_item [' dead ']
# new asymptomatic infection
Infect_add=add_item [' infect]
# outside the input of new
ImportedCase_add=add_item [' importedCase]
# print (date_add, confirm_add heal_add, dead_add, infect_add, importedCase_add)
History_dict [date_add]. Update (
{' confirm_add: confirm_add, 'heal_add: heal_add,' dead_add: dead_add, 'infect_add: infect_add,
'importedCase_add: importedCase_add})
# print (history_dict)
Return history_dict


# 3, data storage
Def save_histroy_data (dic) :
# to create a database link
Conn, cursor=get_conn ()

For the key, val in dic. The items () :
Insert_list=[]
Confirm=dic [key] [' confirm ']
At heal=dic [key] [' at heal]
Dead=dic [key] [' dead ']
Dic nowConfirm=[key] [' nowConfirm]
Dic noInfect=[key] [' noInfect]
Dic importedCase=[key] [' importedCase]
Dic confirm_add=[key] [' confirm_add]
Dic heal_add=[key] [' heal_add]
Dic dead_add=[key] [' dead_add]
Dic infect_add=[key] [' infect_add]
Dic importedCase_add=[key] [' importedCase_add]

Insert_list. Append (key)
Insert_list. Append (confirm)
Insert_list. Append (at heal)
Insert_list. Append (dead)
Insert_list. Append (nowConfirm)
Insert_list. Append (noInfect)
Insert_list. Append (importedCase)
Insert_list. Append (confirm_add)
Insert_list. Append (heal_add)
Insert_list. Append (dead_add)
Insert_list. Append (infect_add)
Insert_list. Append (importedCase_add)
Insert_list. Append (datetime. Datetime. Now (). The strftime (" % % Y - m - H: % d % % m: % S "))
Print (insert_list)
Insert_sql="INSERT INTO history_data (update_time, confirm, at heal, dead, nowConfirm, noInfect, importedCase, confirm_add, heal_add, dead_add, infect_add, importedCase_add, create_time) VALUES (% s, % s, % s, % s, % s, % s, % s, % s, % s, % s, % s, % s, % s)"
# print (" inserted into the database successfully ")
Cursor. The execute (insert_sql insert_list)
MIT ()
conn.com
# close the database link
Close_conn (conn, cursor)
Return


If __name__=="__main__ ':
"' access to all data" '
Qq_data=(https://bbs.csdn.net/topics/get_qq_data)
# print (qq_data)
Dic=get_histroy_data (qq_data)
"'
For dic_item in dic. The items () :
Print (dic_item) "'
Save_histroy_data (dic)
Error:
nullnullnullnullnullnullnullnullnullnull
  • Related