我试图从网站上获取一个值,一个网站上经常发生的变化,我想得到实际的值
我试过这个:
my_url = requests.get('https://www.telekom.hu/shop/categoryresults/https://www.telekom.hu/shop/categoryresults/?N=10994&contractType=list_price&instock_products=1&Ns=sku.sortingPrice%7C0%7C%7Cproduct.displayName%7C0&No=0&Nrpp=9&paymentType=FULL')
data = my_url.text
parsed = json.loads(data)
my_number = parsed["totalNumRecs"]
print my_number
但我得到了这个错误信息:
"my_number = parsed["totalNumRecs"]
KeyError: 'totalNumRecs'"
我怎么了?为什么我无法取回totalNumRecs中的这个号码
您需要指定所需密钥的完整“路径”:
出现键错误的原因是返回的字典的嵌套结构。totalNumRecs事实上存在,但不在目录的顶层。请查看:
Find all occurrences of a key in nested python dictionaries and lists
这是一种遍历未知结构的字典并查找特定键的所有出现情况的方法。受上述链接启发,我通过以下代码找到了您想要的密钥及其值:
相关问题 更多 >
编程相关推荐