python爬取全国疫情数据写入mysql

python爬取全国疫情数据写入mysql

1、实现效果

python爬取全国疫情数据写入mysql_第1张图片

2、实现代码
import pymysql
import requests
import re
import json
from time import sleep
def A():
    try:
        url = "https://services9.arcgis.com/N9p5hsImWXAccRNI/arcgis/rest/services/Nc2JKvYFoAEOFCG5JSI6/FeatureServer/2/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc&resultOffset=0&resultRecordCount=190&resultType=standard&cacheHint=true"
        header = {
            'authority': 'services9.arcgis.com',
            'method': 'GET',
            'scheme': 'https',
            'origin': 'https://gisanddata.maps.arcgis.com',
            'referer': 'https://gisanddata.maps.arcgis.com/apps/opsdashboard/index.html',
            'sec-fetch-site': 'same - site',
            'sec-fetch-mode': 'cors',
            'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
        }
        res = requests.get(url, headers = header)
        res = res.text
        res = json.loads(res)
        db = pymysql.connect(host='127.0.0.1', port=3306,user='root',password= '123456',db='yjc')
        cursor = db.cursor()
        cursor.execute('DROP TABLE IF EXISTS info')
        sql = """CREATE TABLE info (
                 Contury varchar(25) PRIMARY KEY,
                 Confirmed_num varchar(25),
                 Active_num varchar(25),
                 Recovered_num varchar(25),
                 Dead_num varchar(25))"""
        cursor.execute(sql)
        for i in res['features']:
            L = []
            sleep(1)
            Contury = i['attributes']['Country_Region']
            Confirmed_num = i['attributes']['Confirmed']
            Active_num = i['attributes']['Active']
            Recovered_num = i['attributes']['Recovered']
            Dead_num = i['attributes']['Deaths']
            L.append(Contury)
            L.append(Confirmed_num)
            L.append(Active_num)
            L.append(Recovered_num)
            L.append(Dead_num)
            print("国家:",Contury,"确诊人数:",Confirmed_num,"现存人数:",Active_num,"治愈人数:",Recovered_num,"死亡人数:",Dead_num)
            sql = "INSERT INTO info(Contury, Confirmed_num, Active_num, Recovered_num, Dead_num) VALUES (%s,%s,%s,%s,%s)"
            cursor.execute(sql,L)
            db.commit()
        db.close()
    except:
        print("error")
A()

你可能感兴趣的:(python,mysql,json)