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']) + ' ' 'a:' + str(data['Add']['c']) + ' ' 'b:' + str(data['Add']['c']) + ' '] result = ''.join(list) with open('test.txt', 'a+', encoding="utf-8") as f: f.write(result + ' ') 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']) + ' ' ] 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()