我正在尝试访问此 json 的“城市”属性,但不知何故它不起作用,这是 json 结构:
"{\"ForSaleShopperPlatformFullRenderQuery{\\\"zpid\\\":28657235,\\\"platform\\\":\\\"DESKTOP_WEB\\\",\\\"formType\\\":\\\"OPAQUE\\\",\\\"contactFormRenderParameter\\\":{\\\"zpid\\\":28657235,\\\"platform\\\":\\\"desktop\\\",\\\"isDoubleScroll\\\":true},\\\"skipCFRD\\\":false,\\\"ompPlatform\\\":\\\"web\\\"}\":{\"property\":{\"listingDataSource\":\"Phoenix\",\"zpid\":28657235,\"city\":\"Boerne\",\"state\":\"TX\",\"homeStatus\":\"FOR_SALE\",\"address\":{\"streetAddress\":\"111 stone creek\",\"city\":\"Boerne\",\"state\":\"TX\",\"zipcode\":\"78006\",\"neighborhood\":null,\"community\":null,\"subdivision\":null},\"isListingClaimedByCurrentSignedInUser\":false,\"isCurrentSignedInAgentResponsible\":false,\"bedrooms\":3,\"bathrooms\":2,\"price\":345000,\"yearBuilt\":1999,\"streetAddress\":\"111 stone creek\",\"zipcode\":\"78006\",\"isCurrentSignedInUserVerifiedOwner\":false,\"propertyUpdatePageLink\":null,\"moveHomeMapLocationLink\":null,\"propertyEventLogLink\":null,\"editPropertyHistorylink\":null,\"collections\":{\"modules\":[{\"name\":\"Similar homes\",\"placement\":\"NEIGHBORHOOD\",\"propertyDetails\":[{\"miniCardPhotos\":[{\"url\":\"https://photos.zillowstatic.com/fp/219307696092bb2d5e31698cba1c1e1f-p_c.jpg\"}],\"price\":315000,\"currency\":\"USD\",\"bedrooms\":3,\"bathrooms\":3,\"livingArea\":1729,\"livingAreaValue\":1729,\"livingAreaUnits\":\"Square Feet\",\"livingAreaUnitsShort\":\"sqft\",\"listingMetadata\":{\"comminglingCategoryIsRulesApplicable\":true},\"lotSize\":2874,\"lotAreaValue\":2874.96,\"lotAreaUnits\":\"Square Feet\",\"address\":{\"streetAddress\":\"130 Hampton Bend\",\"city\":\"Boerne\",
{
"ForSaleShopperPlatformFullRenderQuery{\"zpid\":28657235,\"platform\":\"DESKTOP_WEB\",\"formType\":\"OPAQUE\",\"contactFormRenderParameter\":{\"zpid\":28657235,\"platform\":\"desktop\",\"isDoubleScroll\":true},\"skipCFRD\":false,\"ompPlatform\":\"web\"}": {
"property": {
"listingDataSource": "Phoenix",
"zpid": 28657235,
"city": "Boerne",
"state": "TX",
"homeStatus": "FOR_SALE",
"address": {
"streetAddress": "111 stone creek",
"city": "Boerne",
"state": "TX",
"zipcode": "78006",
"neighborhood": null,
"community": null,
"subdivision": null
},
这是代码:
key = 'ForSaleShopperPlatformFullRenderQuery{"zpid":28657235,"platform":"DESKTOP_WEB","formType":"OPAQUE","contactFormRenderParameter":{"zpid":28657235,"platform":"desktop","isDoubleScroll":true},"skipCFRD":false,"ompPlatform":"web"}'
city = data["props"]["pageProps"]["componentProps"]["gdpClientCache"][key]['property']['city']
我得到错误:
print(data["props"]["pageProps"]["componentProps"]["gdpClientCache"]['ForSaleShopperPlatformFullRenderQuery{\\"zpid\":28657235,\\"platform\\":\\"DESKTOP_WEB\\",\\"formType\\":\\"OPAQUE\\",\\"contactFormRenderParameter\\":{\\"zpid\\":28657235,\\"platform\\":\\"desktop\\",\\"isDoubleScroll\\":true},\\"skipCFRD\\":false,\\"ompPlatform\\":\\"web\\"}'])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: string indices must be integers, not 'str'
也尝试过|| |还是不行
key = "ForSaleShopperPlatformFullRenderQuery{\"zpid\":28657235,\"platform\":\"DESKTOP_WEB\",\"formType\":\"OPAQUE\",\"contactFormRenderParameter\":{\"zpid\":28657235,\"platform\":\"desktop\",\"isDoubleScroll\":true},\"skipCFRD\":false,\"ompPlatform\":\"web\"}"
still doesn't work
问题出在尝试将字符串用作字典的键,而字典的键应该是整数或字符串,但不能是包含字典的列表。在的 JSON 数据中,
data["props"]["pageProps"]["componentProps"]["gdpClientCache"]
返回一个列表,而尝试使用字符串
key
作为索引访问该列表中的元素。
为了解决这个问题,需要确定想要访问列表中哪个元素,然后使用该元素的索引来访问它。
例如,如果想要访问列表中的第一个元素,可以使用以下代码:
key = 'ForSaleShopperPlatformFullRenderQuery{"zpid":28657235,"platform":"DESKTOP_WEB","formType":"OPAQUE","contactFormRenderParameter":{"zpid":28657235,"platform":"desktop","isDoubleScroll":true},"skipCFRD":false,"ompPlatform":"web"}'
city = data["props"]["pageProps"]["componentProps"]["gdpClientCache"][0][key]['property']['city']
请注意,我们使用
[0]
来访问列表中的第一个元素。如果想要访问其他元素,请相应地更改索引。
此外,还需要确保
key
字符串的值与 JSON 数据中的键完全匹配,包括所有的特殊字符。