1.Python爬取数据保存在嵌套字典里
网页爬取得数据保存为嵌套字典,并将该字典定义为全局变量
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.test import TestCase
import requests
import json
import re
import pandas as pd
import requests
all_parentcard={}
'''****************获取父卡卡片信息:计划、设计、执行、进度并检查父卡片标题和描述区域内容是否统一************************'''
#获取父卡基本信息
def get_parentcard_info(Main_Daily_Info):
global all_parentcard
for pro_card in MainInfo['data']['kanbanLaneCards']:
for card in pro_card['cards']:
pro_par_info = {}
if card["isParentCard"]==True:
pro_par_info["pro_id"] = card["project"]["id"]
pro_par_info["pro_user"] = card["assignee"]["name"]
pro_par_info["description"] = card["description"]
pro_par_info["title"] = card["title"]
all_parentcard[pro_par_info["pro_id"]] = pro_par_info
最后实现的全局变量all_parentcard为嵌套字典,打印出来如下:
ST1={'ST1_jindu': '100%', 'ST1_plan': '01.20-02.28', 'ST1_act': '01.20-03.06'}
ST2= {'ST2_jindu': '100%', 'ST2_plan': '03.02-03.06', 'ST2_act': '03.04-03.13'}
Plan= {'Plan_day': '01.15-01.16', 'Plan_jindu': '100%'}
Design= {'design_jindu': '100%', 'design_plan': '01.17-02.07', 'design_act': '01.17-01.22'}
把上面的四个字典,与上述生成的全局变量all_parentcard嵌套字典合并,放在和Pro_id,pro_user同级:
all_parentcard['TMS-5183'].update(ST1)
all_parentcard['TMS-5183'].update(ST2)
all_parentcard['TMS-5183'].update(Plan)
all_parentcard['TMS-5183'].update(Design)
print(all_parentcard)
合并字典为: