个人作业二

python爬取(一个小模板)

import requests
import json
 
def Down_data():
    url = '网址'
    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']) + '\n'
             'a' + str(data['Add']['c']) + '\n'
             'b' + str(data['Add']['c']) + '\n']
    result = ''.join(list)
    with open('test.txt', 'a+', encoding="utf-8") as f:
        f.write(result + '\n')
 
def Parse_data2():
    data = Down_data()['areaTree'][0]['children']
    path = str(input('test:'))
    for i in data:
        if path in i['name']:
            for item in i['children']:
                list_city = [' a:' + str(item['total']['c']),
                    ' b:' + str(item['today']['c']) + '\n'
                ]
                res_city = ''.join(list_city)
                with open('test.txt', 'a+', encoding="utf-8") as f:
                    f.write(res_city)
 
Down_data()
Parse_data1()
Parse_data2()

 

posted @ 2021-06-10 21:07  谦寻  阅读(32)  评论(0编辑  收藏  举报