我正在尝试从 Python 3.7 中的 json 文件制作的字典中创建一个列表。
json 文件具有以下结构:
watches
collection
model
0 {…}
1
rmc
0 "value_I_need"
1 "value_I_need"
json 提取:
{"watches":{"collection":{"event_banner":{"type":"banner","margin":false,"mobile_layer":true,"class":"tdr-banners-events","media":{"type":"image","src":"/public/banners/events/baselworld_2017_navigation.jpg","height":"150px"},"text":{"align":"left","animate":true,"positioning":"left","suptitle":"BANNER_EVENT_A_TITLE","title":"BANNER_EVENT_A_SUPTITLE","title_type":"h2","style":"light","link_text":"BANNER_EVENT_A_LINK_TEXT","link_href":"/magazine/article/baselworld-2017"}},"collection-navigation":{"type":"view","template":"nav.tdr-collection-navigation.tdr-flex.tdr-flex--align-items-center > ul.tdr-collection-navigation__list.tdr-flex.tdr-flex--align-items-flex-start@list","children":[{"type":"view","template":"li.tdr-collection-navigation__item","insert":{"where":"list"},"children":[{"type":"button-gamma","text":"FIND_YOUR_TUDOR_COLLECTION","href":"/search","cssClass":"tdr-button--gamma-collection-navigation","children":[{"type":"new-
print(documents)
我构建列表的代码:
with open('test.json', 'r') as f:
dictionary = json.load(f)
documents = dictionary["watches"]["collection"]["model"]
for document in documents:
models = document["rmc"]
try:
for model in models:
start_urls.append('https://www.example.com/'+document['page_link']+'/'+model+'.html')
except Exception:
pass
回溯错误:
models = document["rmc"]
TypeError: string indices must be integers
rmc 值是模型列表中的另一个列表。所以每个模型可能有另一个 rmc 值列表。
我的目标是创建所有模型的列表,包括它们的变体 (rmc)。
为什么 pyhton 告诉我它是一个字符串,而我相信 rmc 行以整数形式列出?
慕少森
九州编程
相关分类