zoukankan      html  css  js  c++  java
  • python将爬取数据存储到文本文件


    #author_='zhi';
    #date: 2020/2/10 17:01

    import requests
    import json

    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()
    list = ['截至时间:' + 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']) + ' ']
    result = ''.join(list)
    with open('疫情查询.txt', 'a+', encoding="utf-8") as f:
    f.write(result + ' ')

    def Parse_data2():
    data = Down_data()['areaTree'][0]['children']
    path = str(input('请输入你要查询的省份:'))
    for i in data:
    if path in i['name']:
    for item in i['children']:
    list_city = [
    '地区: ' + str(item['name']) + ' '
    ' 确诊人数:' + str(item['total']['confirm']),
    ' 新增确诊:' + str(item['today']['confirm']),
    ' 治愈:' + str(item['total']['heal']),
    ' 新增治愈:' + str(item['today']['heal']),
    ' 死亡:' + str(item['total']['dead']),
    ' 新增死亡:' + str(item['today']['dead']) + ' '
    ]
    res_city = ''.join(list_city)
    with open('疫情查询.txt', 'a+', encoding="utf-8") as f:
    f.write(res_city)

    Down_data()
    Parse_data1()


    原文链接:https://blog.csdn.net/qq_42680327/article/details/104251385

  • 相关阅读:
    POJ3253Fence Repair(优先队列或单调队列)
    POJ3630Phone List(字典树)
    HDU1896Stones(优先队列)
    POJ3468 A Simple Problem with Integers(线段树延时标记)
    HDU3535AreYouBusy(分组背包)
    C++ 学习涨姿势汇总
    [C++] std::vector 使用
    Cocos2dx-3.2 引擎学习(四)之CCScheduler
    Cocos2dx-3.2 引擎学习(三)之AssetsManager
    Cocos2dx-3.2 引擎学习(二)之Director
  • 原文地址:https://www.cnblogs.com/1329197745a/p/14899738.html
Copyright © 2011-2022 走看看