2017-12-14 7 views
-1

私はZillowのAPIを呼び出し、XMLで応答を返す次のコードを持っています。 XMLを辞書に変換しますが、特定の要素にアクセスする際に問題が発生しています。応答に "zpid"値を含む変数を作成するにはどうすればよいですか?ここでPythonでOrderedDictの特定の要素にアクセスする

は私のコードです:

import requests 
import xmltodict, json 
import collections 
API_KEY = "KEY_HIDDEN_FOR_SECURITY_REASONS" 
DEEP_COMP_BASE_URL = "http://www.zillow.com/webservice/GetDeepComps.htm?zws-id=" + API_KEY +"&zpid=48749425&count=5" 
DEEP_SEARCH_RESULTS_BASE_URL = "http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=" + API_KEY + "&address=2114+Bigelow+Ave&citystatezip=Seattle%2C+WA" 

def organize_json(json_thing, sort=True, indents=4): 
    if type(json_thing) is str: 
     print(json.dumps(json.loads(json_thing), sort_keys=sort, indent=indents)) 
    else: 
     print(json.dumps(json_thing, sort_keys=sort, indent=indents)) 
    return None 


def get_deep_comps(): 
    response = requests.get(DEEP_COMP_BASE_URL) 
    content = xmltodict.parse(response.content) 
    print content 

def get_zpid_from_address(): 
    print("Finding address...") 


get_deep_comps() 

そして出力:

OrderedDict([(u'Comps:comps', OrderedDict([(u'@xsi:schemaLocation', u'http://www.zillow.com/static/xsd/Comps.xsd https://www.zillowstatic.com/vstatic/6ce354c/static/xsd/Comps.xsd'), (u'@xmlns:xsi', u'http://www.w3.org/2001/XMLSchema-instance'), (u'@xmlns:Comps', u'http://www.zillow.com/static/xsd/Comps.xsd'), (u'request', OrderedDict([(u'zpid', u'48749425'), (u'count', u'5')])), (u'message', OrderedDict([(u'text', u'Request successfully processed'), (u'code', u'0')])), (u'response', OrderedDict([(u'properties', OrderedDict([(u'principal', OrderedDict([(u'zpid', u'48749425'), (u'links', OrderedDict([(u'homedetails', u'https://www.zillow.com/homedetails/2114-Bigelow-Ave-N-Seattle-WA-98109/48749425_zpid/'), (u'graphsanddata', u'http://www.zillow.com/homedetails/2114-Bigelow-Ave-N-Seattle-WA-98109/48749425_zpid/#charts-and-data'), (u'mapthishome', u'http://www.zillow.com/homes/48749425_zpid/'), (u'comparables', u'http://www.zillow.com/homes/comps/48749425_zpid/')])), (u'address', OrderedDict([(u'street', u'2114 Bigelow Ave N'), (u'zipcode', u'98109'), (u'city', u'Seattle'), (u'state', u'WA'), (u'latitude', u'47.637933'), (u'longitude', u'-122.347938')])), (u'taxAssessmentYear', u'2016'), (u'taxAssessment', u'1226000.0'), (u'yearBuilt', u'1924'), (u'lotSizeSqFt', u'4680'), (u'finishedSqFt', u'3470'), (u'bathrooms', u'3.0'), (u'bedrooms', u'4'), (u'lastSoldDate', u'11/26/2008'), (u'lastSoldPrice', OrderedDict([(u'@currency', u'USD'), ('#text', u'995000')])), (u'zestimate', OrderedDict([(u'amount', OrderedDict([(u'@currency', u'USD'), ('#text', u'2112753')])), (u'last-updated', u'12/12/2017'), (u'oneWeekChange', OrderedDict([(u'@deprecated', u'true')])), (u'valueChange', OrderedDict([(u'@duration', u'30'), (u'@currency', u'USD'), ('#text', u'10681')])), (u'valuationRange', OrderedDict([(u'low', OrderedDict([(u'@currency', u'USD'), ('#text', u'2007115')])), (u'high', OrderedDict([(u'@currency', u'USD'), ('#text', u'2218391')]))])), (u'percentile', u'98')])), (u'localRealEstate', OrderedDict([(u'region', OrderedDict([(u'@name', u'East Queen Anne'), (u'@id', u'271856'), (u'@type', u'neighborhood'), (u'zindexValue', u'821,600'), (u'links', OrderedDict([(u'overview', u'http://www.zillow.com/local-info/WA-Seattle/East-Queen-Anne/r_271856/'), (u'forSaleByOwner', u'http://www.zillow.com/east-queen-anne-seattle-wa/fsbo/'), (u'forSale', u'http://www.zillow.com/east-queen-anne-seattle-wa/')]))]))]))])), (u'comparables', OrderedDict([(u'comp', [OrderedDict([(u'@score', u'11.0'), (u'zpid', u'48690076'), (u'links', OrderedDict([(u'homedetails', u'https://www.zillow.com/homedetails/309-Newton-St-Seattle-WA-98109/48690076_zpid/'), (u'graphsanddata', u'http://www.zillow.com/homedetails/309-Newton-St-Seattle-WA-98109/48690076_zpid/#charts-and-data'), (u'mapthishome', u'http://www.zillow.com/homes/48690076_zpid/'), (u'comparables', u'http://www.zillow.com/homes/comps/48690076_zpid/')])), (u'address', OrderedDict([(u'street', u'309 Newton St'), (u'zipcode', u'98109'), (u'city', u'Seattle'), (u'state', u'WA'), (u'latitude', u'47.636324'), (u'longitude', u'-122.351433')])), (u'taxAssessmentYear', u'2016'), (u'taxAssessment', u'900000.0'), (u'yearBuilt', u'1924'), (u'lotSizeSqFt', u'4356'), (u'finishedSqFt', u'2160'), (u'bathrooms', u'1.1'), (u'bedrooms', u'3'), (u'lastSoldDate', u'06/22/2017'), (u'lastSoldPrice', OrderedDict([(u'@currency', u'USD'), ('#text', u'1200000')])), (u'zestimate', OrderedDict([(u'amount', OrderedDict([(u'@currency', u'USD'), ('#text', u'1228902')])), (u'last-updated', u'12/12/2017'), (u'oneWeekChange', OrderedDict([(u'@deprecated', u'true')])), (u'valueChange', OrderedDict([(u'@duration', u'30'), (u'@currency', u'USD'), ('#text', u'11596')])), (u'valuationRange', OrderedDict([(u'low', OrderedDict([(u'@currency', u'USD'), ('#text', u'1167457')])), (u'high', OrderedDict([(u'@currency', u'USD'), ('#text', u'1290347')]))])), (u'percentile', u'94')])), (u'localRealEstate', OrderedDict([(u'region', OrderedDict([(u'@name', u'East Queen Anne'), (u'@id', u'271856'), (u'@type', u'neighborhood'), (u'zindexValue', u'821,600'), (u'links', OrderedDict([(u'overview', u'http://www.zillow.com/local-info/WA-Seattle/East-Queen-Anne/r_271856/'), (u'forSaleByOwner', u'http://www.zillow.com/east-queen-anne-seattle-wa/fsbo/'), (u'forSale', u'http://www.zillow.com/east-queen-anne-seattle-wa/')]))]))]))]), OrderedDict([(u'@score', u'11.0'), (u'zpid', u'48689963'), (u'links', OrderedDict([(u'homedetails', u'https://www.zillow.com/homedetails/361-Blaine-St-Seattle-WA-98109/48689963_zpid/'), (u'graphsanddata', u'http://www.zillow.com/homedetails/361-Blaine-St-Seattle-WA-98109/48689963_zpid/#charts-and-data'), (u'mapthishome', u'http://www.zillow.com/homes/48689963_zpid/'), (u'comparables', u'http://www.zillow.com/homes/comps/48689963_zpid/')])), (u'address', OrderedDict([(u'street', u'361 Blaine St'), (u'zipcode', u'98109'), (u'city', u'Seattle'), (u'state', u'WA'), (u'latitude', u'47.634705'), (u'longitude', u'-122.350054')])), (u'taxAssessmentYear', u'2016'), (u'taxAssessment', u'886000.0'), (u'yearBuilt', u'1919'), (u'lotSizeSqFt', u'2500'), (u'finishedSqFt', u'2580'), (u'bathrooms', u'2.0'), (u'bedrooms', u'4'), (u'lastSoldDate', u'03/24/2017'), (u'lastSoldPrice', OrderedDict([(u'@currency', u'USD'), ('#text', u'1200000')])), (u'zestimate', OrderedDict([(u'amount', OrderedDict([(u'@currency', u'USD'), ('#text', u'1253607')])), (u'last-updated', u'12/12/2017'), (u'oneWeekChange', OrderedDict([(u'@deprecated', u'true')])), (u'valueChange', OrderedDict([(u'@duration', u'30'), (u'@currency', u'USD'), ('#text', u'-7238')])), (u'valuationRange', OrderedDict([(u'low', OrderedDict([(u'@currency', u'USD'), ('#text', u'1190927')])), (u'high', OrderedDict([(u'@currency', u'USD'), ('#text', u'1316287')]))])), (u'percentile', u'94')])), (u'localRealEstate', OrderedDict([(u'region', OrderedDict([(u'@name', u'East Queen Anne'), (u'@id', u'271856'), (u'@type', u'neighborhood'), (u'zindexValue', u'821,600'), (u'links', OrderedDict([(u'overview', u'http://www.zillow.com/local-info/WA-Seattle/East-Queen-Anne/r_271856/'), (u'forSaleByOwner', u'http://www.zillow.com/east-queen-anne-seattle-wa/fsbo/'), (u'forSale', u'http://www.zillow.com/east-queen-anne-seattle-wa/')]))]))]))]), OrderedDict([(u'@score', u'7.0'), (u'zpid', u'48690235'), (u'links', OrderedDict([(u'homedetails', u'https://www.zillow.com/homedetails/2223-4th-Ave-N-Seattle-WA-98109/48690235_zpid/'), (u'graphsanddata', u'http://www.zillow.com/homedetails/2223-4th-Ave-N-Seattle-WA-98109/48690235_zpid/#charts-and-data'), (u'mapthishome', u'http://www.zillow.com/homes/48690235_zpid/'), (u'comparables', u'http://www.zillow.com/homes/comps/48690235_zpid/')])), (u'address', OrderedDict([(u'street', u'2223 4th Ave N'), (u'zipcode', u'98109'), (u'city', u'Seattle'), (u'state', u'WA'), (u'latitude', u'47.638955'), (u'longitude', u'-122.34978')])), (u'taxAssessmentYear', u'2016'), (u'taxAssessment', u'1044000.0'), (u'yearBuilt', u'1907'), (u'lotSizeSqFt', u'4000'), (u'finishedSqFt', u'2770'), (u'bathrooms', u'3.0'), (u'bedrooms', u'4'), (u'totalRooms', u'9'), (u'lastSoldDate', u'03/30/2017'), (u'lastSoldPrice', OrderedDict([(u'@currency', u'USD'), ('#text', u'1325000')])), (u'zestimate', OrderedDict([(u'amount', OrderedDict([(u'@currency', u'USD'), ('#text', u'1412136')])), (u'last-updated', u'12/12/2017'), (u'oneWeekChange', OrderedDict([(u'@deprecated', u'true')])), (u'valueChange', OrderedDict([(u'@duration', u'30'), (u'@currency', u'USD'), ('#text', u'7432')])), (u'valuationRange', OrderedDict([(u'low', OrderedDict([(u'@currency', u'USD'), ('#text', u'1341529')])), (u'high', OrderedDict([(u'@currency', u'USD'), ('#text', u'1482743')]))])), (u'percentile', u'95')])), (u'localRealEstate', OrderedDict([(u'region', OrderedDict([(u'@name', u'East Queen Anne'), (u'@id', u'271856'), (u'@type', u'neighborhood'), (u'zindexValue', u'821,600'), (u'links', OrderedDict([(u'overview', u'http://www.zillow.com/local-info/WA-Seattle/East-Queen-Anne/r_271856/'), (u'forSaleByOwner', u'http://www.zillow.com/east-queen-anne-seattle-wa/fsbo/'), (u'forSale', u'http://www.zillow.com/east-queen-anne-seattle-wa/')]))]))]))]), OrderedDict([(u'@score', u'3.0'), (u'zpid', u'48749401'), (u'links', OrderedDict([(u'homedetails', u'https://www.zillow.com/homedetails/2308-Bigelow-Ave-N-Seattle-WA-98109/48749401_zpid/'), (u'graphsanddata', u'http://www.zillow.com/homedetails/2308-Bigelow-Ave-N-Seattle-WA-98109/48749401_zpid/#charts-and-data'), (u'mapthishome', u'http://www.zillow.com/homes/48749401_zpid/'), (u'comparables', u'http://www.zillow.com/homes/comps/48749401_zpid/')])), (u'address', OrderedDict([(u'street', u'2308 Bigelow Ave N'), (u'zipcode', u'98109'), (u'city', u'Seattle'), (u'state', u'WA'), (u'latitude', u'47.639391'), (u'longitude', u'-122.34791')])), (u'taxAssessmentYear', u'2016'), (u'taxAssessment', u'1699000.0'), (u'yearBuilt', u'1927'), (u'lotSizeSqFt', u'5850'), (u'finishedSqFt', u'3160'), (u'bathrooms', u'4.0'), (u'bedrooms', u'3'), (u'lastSoldDate', u'06/28/2017'), (u'lastSoldPrice', OrderedDict([(u'@currency', u'USD'), ('#text', u'2300000')])), (u'zestimate', OrderedDict([(u'amount', OrderedDict([(u'@currency', u'USD'), ('#text', u'2363106')])), (u'last-updated', u'12/12/2017'), (u'oneWeekChange', OrderedDict([(u'@deprecated', u'true')])), (u'valueChange', OrderedDict([(u'@duration', u'30'), (u'@currency', u'USD'), ('#text', u'2217')])), (u'valuationRange', OrderedDict([(u'low', OrderedDict([(u'@currency', u'USD'), ('#text', u'2244951')])), (u'high', OrderedDict([(u'@currency', u'USD'), ('#text', u'2481261')]))])), (u'percentile', u'99')])), (u'localRealEstate', OrderedDict([(u'region', OrderedDict([(u'@name', u'East Queen Anne'), (u'@id', u'271856'), (u'@type', u'neighborhood'), (u'zindexValue', u'821,600'), (u'links', OrderedDict([(u'overview', u'http://www.zillow.com/local-info/WA-Seattle/East-Queen-Anne/r_271856/'), (u'forSaleByOwner', u'http://www.zillow.com/east-queen-anne-seattle-wa/fsbo/'), (u'forSale', u'http://www.zillow.com/east-queen-anne-seattle-wa/')]))]))]))]), OrderedDict([(u'@score', u'5.0'), (u'zpid', u'48749412'), (u'links', OrderedDict([(u'homedetails', u'https://www.zillow.com/homedetails/459-Lynn-St-Seattle-WA-98109/48749412_zpid/'), (u'graphsanddata', u'http://www.zillow.com/homedetails/459-Lynn-St-Seattle-WA-98109/48749412_zpid/#charts-and-data'), (u'mapthishome', u'http://www.zillow.com/homes/48749412_zpid/'), (u'comparables', u'http://www.zillow.com/homes/comps/48749412_zpid/')])), (u'address', OrderedDict([(u'street', u'459 Lynn St'), (u'zipcode', u'98109'), (u'city', u'Seattle'), (u'state', u'WA'), (u'latitude', u'47.638884'), (u'longitude', u'-122.347773')])), (u'taxAssessmentYear', u'2016'), (u'taxAssessment', u'1197000.0'), (u'yearBuilt', u'1923'), (u'lotSizeSqFt', u'3900'), (u'finishedSqFt', u'2800'), (u'bathrooms', u'2.0'), (u'bedrooms', u'4'), (u'lastSoldDate', u'07/17/2017'), (u'lastSoldPrice', OrderedDict([(u'@currency', u'USD'), ('#text', u'1615000')])), (u'zestimate', OrderedDict([(u'amount', OrderedDict([(u'@currency', u'USD'), ('#text', u'1649179')])), (u'last-updated', u'12/12/2017'), (u'oneWeekChange', OrderedDict([(u'@deprecated', u'true')])), (u'valueChange', OrderedDict([(u'@duration', u'30'), (u'@currency', u'USD'), ('#text', u'5999')])), (u'valuationRange', OrderedDict([(u'low', OrderedDict([(u'@currency', u'USD'), ('#text', u'1566720')])), (u'high', OrderedDict([(u'@currency', u'USD'), ('#text', u'1731638')]))])), (u'percentile', u'97')])), (u'localRealEstate', OrderedDict([(u'region', OrderedDict([(u'@name', u'East Queen Anne'), (u'@id', u'271856'), (u'@type', u'neighborhood'), (u'zindexValue', u'821,600'), (u'links', OrderedDict([(u'overview', u'http://www.zillow.com/local-info/WA-Seattle/East-Queen-Anne/r_271856/'), (u'forSaleByOwner', u'http://www.zillow.com/east-queen-anne-seattle-wa/fsbo/'), (u'forSale', u'http://www.zillow.com/east-queen-anne-seattle-wa/')]))]))]))])])]))]))]))]))]) 

Pythonでこれをアプローチする簡潔な方法は何ですか?

+1

どのようにJavascriptで行いますか?同じようなテクニックがここで働くかもしれません。 – larsks

+0

@larsks私は質問とデータの構造を変更しました。私は自分の言い回しをJSONに変換するのが間違ったアプローチだと思う。 – vipertherapper

+2

Python辞書はJavascriptオブジェクトとよく似ています。あなたは 'content [' somekey '] [0] [' anotherkey '] 'などを求めることができます。 – larsks

答えて

1

私はあなたのコードを試してみましたが、ちょうどnone私の中でzipidの値を、取得するには次の順序の辞書

OrderedDict([('Comps:comps', OrderedDict([('@xsi:schemaLocation', 'http://www.zillow.com/static/xsd/Comps.xsd https://www.zillowstatic.com/vstatic/6ce354c/static/xsd/Comps.xsd'), ('@xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'), ('@xmlns:Comps', 'http://www.zillow.com/static/xsd/Comps.xsd'), ('request', OrderedDict([('zpid', None), ('count', None)])), ('message', OrderedDict([('text', 'Error: invalid or missing ZWSID parameter'), ('code', '2')]))]))]) 

、その結果応答内容

<?xml version="1.0" encoding="utf-8"?> 
<Comps:comps xsi:schemaLocation="http://www.zillow.com/static/xsd/Comps.xsd https://www.zillowstatic.com/vstatic/6ce354c/static/xsd/Comps.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:Comps="http://www.zillow.com/static/xsd/Comps.xsd"> 
    <request> 
     <zpid></zpid> 
     <count></count> 
    </request> 
    <message> 
     <text>Error: invalid or missing ZWSID parameter</text> 
     <code>2</code> 
    </message> 
</Comps:comps> 

としてこのXMLを得ましたたとえば、次の行を使用できます。

zpid=content['Comps:comps']['request']['zpid'] 
関連する問題