• android 学习


    今天学习了如何用python爬取数据

    用python爬取了疫情数据并存到数据库里

    import requests
    import json
    import pymysql
    
    # 建立游标
    
    # 数据库操作
    # (1)定义一个格式化的sql语句
    sql = 'insert into mm(姓名,性别) values(%s,%s) '
    # (2)准备数据
    data = ('nancy', '30')
    # (3)操作
    
    def Down_data():
        url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
        headers = {
            'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36'
        }
        r = requests.get(url, headers)
        res = json.loads(r.text)
        data_res = json.loads(res['data'])
        return data_res
    
    
    def Parse_data1():
        data = Down_data()
        conn = pymysql.connect(host="localhost", user="root", password="20194015", database="cov2021", charset='utf8')
        cursor = conn.cursor()
        sql='insert into china(截止时间,全国确诊人数,今日新增确诊,全国疑似,今日新增疑似,全国治愈,今日新增治愈,全国死亡,今日新增死亡) value(%s,%s,%s,%s,%s,%s,%s,%s,%s)'
        num=(str(data['lastUpdateTime']),str(data['chinaTotal']['confirm']),str(data['chinaAdd']['confirm']),str(data['chinaTotal']['suspect']),str(
            data['chinaAdd']['suspect']),str(data['chinaTotal']['heal']),str(
            data['chinaAdd']['heal']),str(data['chinaTotal']['dead']),str(
            data['chinaAdd']['dead']))
        try:
            cursor.execute(sql, num)
            conn.commit()
        except Exception as e:
            print('插入数据失败', e)
            conn.rollback()  # 回滚
            cursor.close()
            conn.close()
    
    
    
    def Parse_data2():
        data = Down_data()['areaTree'][0]['children']
        data1 = Down_data()
    
        path = str()
        for i in data:
            if path in i['name']:
                for item in i['children']:
                    conn = pymysql.connect(host="localhost", user="root", password="20194015", database="cov2021",
                                           charset='utf8')
                    cursor = conn.cursor()
                    sql = 'insert into diqu(截止时间,地区,确诊人数,新增确诊,治愈,死亡) value(%s,%s,%s,%s,%s,%s)'
                    num = (str(data1['lastUpdateTime']), str(item['name']), str(item['total']['confirm']),
                           str(item['today']['confirm']), str(item['total']['heal']), str(item['total']['dead']))
                    try:
                        cursor.execute(sql, num)
                        conn.commit()
                    except Exception as e:
                        print('插入数据失败', e)
                        conn.rollback()  # 回滚
        cursor.close()
        conn.close()
    
    
    def Parse_data3():
        data3 = Down_data()
        data = Down_data()['areaTree'][0]['children']
        path = str()
    
        for i in data:
            if path in i['name']:
                    conn3 = pymysql.connect(host="localhost", user="root", password="20194015", database="cov2021",
                                            charset='utf8')
                    cursor3 = conn3.cursor()
                    sql3 = 'insert into sheng(截止时间,地区,确诊人数,新增确诊,治愈,死亡) values(%s,%s,%s,%s,%s,%s) '
    
                    data5 =(str(data3['lastUpdateTime']), i['name'], i['total']['confirm'], i['today']['confirm'], i['total']['heal'], i['total']['dead'])
    
                    try:
                        cursor3.execute(sql3, data5)
                        conn3.commit()
                    except Exception as e:
                        print('插入数据失败', e)
                        conn3.rollback()  # 回滚
    
                    # 关闭游标
                    cursor3.close()
                    # 关闭连接
                    conn3.close()
    
    Down_data()
    #Parse_data1()
    #Parse_data2()
    Parse_data3()

  • 相关阅读:
    解决mysql"Access denied for user'root'@'IP地址'"问题
    SpringMVC in IDEA开发实践
    Tomcat安装笔记(on Mac)
    mysql搭建及数据迁移教程
    remove-k-digits
    hdu 4691 Front compression
    STM32的FSMC总线复用调试笔记
    spring AOP 是如何一步一步被简化的
    hdu 2871 Memory Control(伸展树splay tree)
    [置顶] Objective-C ,/,ios,/iphone开发基础:协议(protocol)
  • 原文地址:https://www.cnblogs.com/Nojava/p/14908166.html
Copyright © 2020-2023  润新知