-
数据格式
-
主函数代码
-
爬取数据结果
-
完整代码
import csv, requests # 导入请求模块
import pymysql
def save_html(a):
db = pymysql.connect('localhost', 'root', 'root', 'spider', charset='utf8')
cursor = db.cursor()
ins = 'insert into coronavirus values(%s, %s,%s, %s, %s, %s, %s)'
cursor.execute(ins, a)
db.commit()
cursor.close()
class A(object):
def __init__(self): # 定义获取数据并写入csv文件里的函数
url = "https://api.inews.qq.com/newsqa/v1/automation/foreign/country/ranklist" # 请求网址
response = requests.get(url).json() # 发出请求并json化处理
data = response['data'] # 提取数据部分
for i in range(16):
print(data[i])
print(data[i]['name'], data[i]['confirm'], data[i]['confirmAdd'])
with open("CoronavirusData.csv", "w+", newline="") as json_file:
writer = csv.writer(json_file)
header = ["date", "state", "continent", "confirm", "confirmAdd", "suspect", "dead", "heal"] # 定义表头
writer.writerow(header)
for i in range(len(data)):
data_row1 = [data[i]["date"], data[i]["name"], data[i]["continent"], data[i]["confirm"],
data[i]["confirmAdd"],
data[i]["suspect"], data[i]["dead"], data[i]["heal"]]
writer.writerow(data_row1)
save_html(data_row1)
if __name__ == "__main__":
spider = A()
spider.__init__()
在这里插入代码片