我在用Python解析Google Civic Info API时遇到问题。我已经进行了广泛的搜索,但是无法在Python中找到任何代码。
在谷歌API思域列出两个字典选出的代表。第一个是“办公室”,列出办公室的名称和“ officialIndices”的字典:[2,3],以映射到另一个标记为“ officials”的字典,该字典存储民选官员的信息,例如姓名等。 >
我已经能够使用带有zip()的for循环来映射大多数办公室/官员,但是这会跳过并跳过具有多个官员的办公室,例如美国参议员,因为每个州都有2个办公室。
有人可以为此提供python代码吗?我想列出每个办公室以及担任该办公室的每个人。
Google API响应:
clang++.exe
我的代码:
"offices": [
{
"name": "U.S. Senator",
"divisionId": "ocd-division/country:us/state:tx",
"levels": [
"country"
],
"roles": [
"legislatorUpperBody"
],
"officialIndices": [
2,
3
]
},
.....
"officials": [
{...},
{...},
{
"name": "John Cornyn",
"address": [
{
"line1": "517 Hart Senate Office Building",
"city": "Washington",
"state": "DC",
"zip": "20510"
}
],
"party": "Republican Party",
"phones": [
"(202) 224-2934"
],
"urls": [
"https://www.cornyn.senate.gov/"
],
"photoUrl": "http://bioguide.congress.gov/bioguide/photo/C/C001056.jpg",
"channels": [
{
"type": "Facebook",
"id": "Sen.JohnCornyn"
},
{
"type": "Twitter",
"id": "JohnCornyn"
},
{
"type": "YouTube",
"id": "SenJohnCornyn"
}
]
},
{
"name": "Ted Cruz",
"address": [
{
"line1": "127A",
"city": "Washington",
"state": "DC",
"zip": "20510"
}
],
"party": "Republican Party",
"phones": [
"(202) 224-5922"
],
"urls": [
"https://www.cruz.senate.gov/"
],
"photoUrl": "http://www.cruz.senate.gov/files/images/OfficialPortrait.jpg",
"channels": [
{
"type": "Facebook",
"id": "SenatorTedCruz"
},
{
"type": "Twitter",
"id": "SenTedCruz"
},
{
"type": "YouTube",
"id": "TedCruzforSenate"
},
{
"type": "YouTube",
"id": "SenTedCruz"
}
]
},
打印出:
response = requests.get(url)
data = response.json()
officials = data['officials']
offices = data['offices']
for office, official in zip(offices, officials):
office_name = office['name']
official_name = official['name']
print(office['name'])
print(official['name'])