AttributeError: 'unicode' object has no attribute XXX
AttributeError: 'unicode' object has no attribute XXX
我正在尝试使用 pipl。我想从邮件中获取信息并将其保存到 Excel 文件中。
from piplapis.search import SearchAPIRequest
from piplapis.search import SearchAPIError
from openpyxl import load_workbook
request = SearchAPIRequest(email=u'clark.kent@example.com', api_key='......')
try:
response = request.send()
except SearchAPIError as e:
print e.http_status_code, e
filename = "mails.xlsx"
wb = load_workbook(filename = filename)
ws = wb['Sheet1']
print(response.raw_json)
returns
{
"@http_status_code": 200,
"@visible_sources": 3,
"@available_sources": 3,
"@persons_count": 1,
"@search_id": "0",
"query": {
"emails": [
{
"address": "clark.kent@example.com",
"address_md5": "2610ee49440fe757e3cc4e46e5b40819"
}
]
},
"available_data": {
"premium": {
"relationships": 6,
"usernames": 2,
"jobs": 3,
"addresses": 2,
"ethnicities": 3,
"phones": 1,
"landline_phones": 1,
"educations": 2,
"languages": 1,
"user_ids": 1,
"social_profiles": 3,
"names": 3,
"dobs": 1,
"images": 2,
"genders": 1,
"emails": 4,
"origin_countries": 1
}
},
"person": {
"@id": "bdc8d748-d7a6-454f-adc0-3b3bd0dd5541",
"@match": 1.0,
"@search_pointer": "1b6b0e83e0c1454d7ba38f7aa219162fcefc67a75ae7397ae44e5b99f159997ea310810c0072a53863abfe151d1885bd5774191ee8cb67aec7877b50d30954c68aaf38129d27caa48562bb006b6be824035d6b13e96be99999d39afe6d758a9b5b212c69eac661d7d59209fe80a61854b5ce40e759c96947bfbce63ae9fef6f5eb8a4cde91d717eeb4d8b888db3ac532a5dea8983c12a5d75f57883b5897f25ed1b66ea17e0e733a020e9f35c02bfb98d724d34c18cae07edca937895cecd534b373c764fd385df7a94f72d08c67ba00a50789bec39986676bb25349f954557ce6e7f69de53984f7cf3e7834444739302998418bf16a70cdee9967bd89e8153bf9fc4f1d5f468e1967f7e17601b1f61d9cb497720addf7e7d1406c03c00af42bfa695d209d831203f812c5a9189f9d45cb75912083182df8b0aab3442b050de5c9fc97aabeb25cd43384c6cdf1af1b160c0d829322a3c9430dbf676c541e00212d484bca4f6b364576f08eea5b97ffe6ea7f991098ba1fbe1ae4f2dad46e7ea239076ba9edd333f9562018bef7fe4e6a967a57dfdd19143e4952f2bae5d889211e25ca7292929e83a68dcb677fb4919297f5a53bac6dc9a20d0f7dff88b1323c45ccf7ac79fb9733a1b2e86fcb8fa75d7b788c1c31462feed9c2d3cc4ec6dc72aca57cfb867fca178c668c00b83ed4d45545c424b3ab97d641593c6d87ac7d595bcf7b028b7d315938e9c0f9dcae7ff799c195b08f2464d3f7222bf1c832cabe2998ad1745db8e59a101b26525dac4b56c7b7ea0b33f47814e87a941266a8e136026064ceb196ebe63c6f3352dfe06ddcef028528c9107d276e5f24c47dd6625217545f3564be287e9047122017b0bb2ecefc905b1e049ec6160d824b2ca6edf52ed4cc4b7a3a7665cb45c28b52c72ef24d36b0c1c6974911fb4bb704682e415094294962eaffca26aa61a8469e5815b02253e54f2f5ddd68eada74fcef090d24d7f491126e6fcfec85345a42e7cbb4b22222c89cc04aa94b530a57c35202ffa9c3fd341f19cfe41c4f47e3c541d83f855945ae2cec1966f9c2681eed7be6270bbe902333d8cecf8a805b26eb8e9dd9a0d7c569a82875918add85d9ed5906d8cf0048ea1912df8bf29eed3a851160b13ba8e87b2ca0b119d3351e1bfc44fe67ab440b236e54b1c2d4bc00ca5856803b85caa708d9337d0add706986653c04e9d56a242c6237bb118397ec1b28ed2cac7d3e3019acbdbd2b8ca1a1aae803c959c5ee57e7f3ef30394d62077ecfc7baffdff5cafe0f4da9037490097a45225419a85fcdd6d3a4fc364ac59df4298a22553db8e0dd1c4e9fa628b40fb409ed6c1d60bf403913644f6d5c57d70737f623a45afd4ed533010585f5208338dc26d560369e6843082ab941e7024ecf5908f458d6478d5745ebde8e60ed5ead82e98f38b302157c8316edbaf4572739a21461cb30d791b874c7b56bdba1f0d5ddc3d7902018336dfe7cc9d364d234395b4cf9dc74a7fbc929d7108d43c27b482d1bafea99277476be0c1933ff30f46a498184b7f2d9da6196b63a427ef14c9e1182fbea0e608988d89129d9b7a0b6094bd39a3ec",
"names": [
{
"first": "Kal",
"last": "El",
"display": "Kal El"
},
{
"first": "Clark",
"middle": "Joseph",
"last": "Kent",
"display": "Clark Joseph Kent"
},
{
"display": "The red blue blur"
}
],
"emails": [
{
"@type": "work",
"@email_provider": false,
"address": "clark.kent@thedailyplanet.com",
"address_md5": "eb3e11de3c9cefc2d9d70972350e2b28"
},
{
"@disposable": true,
"@email_provider": false,
"address": "ck242@guerrillamail.com",
"address_md5": "999e509752141a0ee42ff455529c10fc"
},
{
"@type": "personal",
"@email_provider": true,
"address": "clark@gmail.com",
"address_md5": "501548362894b9a08f071b1565d8aa14"
},
{
"@disposable": true,
"@email_provider": false,
"address": "clark.kent@example.com",
"address_md5": "2610ee49440fe757e3cc4e46e5b40819"
}
],
"usernames": [
{
"content": "superman@facebook"
},
{
"content": "@ckent"
}
],
"phones": [
{
"@type": "home_phone",
"country_code": 1,
"number": 9785550145,
"display": "978-555-0145",
"display_international": "+1 978-555-0145"
}
],
"gender": {
"content": "male"
},
"dob": {
"date_range": {
"start": "1986-01-01",
"end": "1987-05-13"
},
"display": "32 years old"
},
"languages": [
{
"region": "US",
"language": "en",
"display": "en_US"
}
],
"ethnicities": [
{
"content": "other"
},
{
"content": "american_indian"
},
{
"content": "white"
}
],
"origin_countries": [
{
"country": "US"
}
],
"addresses": [
{
"@valid_since": "2005-02-12",
"@type": "work",
"country": "US",
"state": "KS",
"city": "Metropolis",
"street": "Broadway",
"house": "1000",
"apartment": "355",
"display": "1000-355 Broadway, Metropolis, Kansas"
},
{
"@valid_since": "1999-02-01",
"@type": "home",
"country": "US",
"state": "KS",
"city": "Smallville",
"street": "Hickory Lane",
"house": "10",
"apartment": "1",
"zip_code": "66605",
"display": "10-1 Hickory Lane, Smallville, Kansas"
}
],
"jobs": [
{
"title": "Field Reporter",
"organization": "The Daily Planet",
"industry": "Journalism",
"date_range": {
"start": "2000-12-08",
"end": "2012-10-09"
},
"display": "Field Reporter at The Daily Planet (2000-2012)"
},
{
"title": "Junior Reporter",
"organization": "The Daily Planet",
"industry": "Journalism",
"date_range": {
"start": "1999-10-10",
"end": "2000-10-10"
},
"display": "Junior Reporter at The Daily Planet (1999-2000)"
},
{
"title": "Top Reporter",
"organization": "The Daily Planet",
"industry": "Reporting",
"display": "Top Reporter at The Daily Planet"
}
],
"educations": [
{
"degree": "B.Sc Advanced Science",
"school": "Metropolis University",
"date_range": {
"start": "2005-09-01",
"end": "2008-05-14"
},
"display": "B.Sc Advanced Science from Metropolis University (2005-2008)"
},
{
"school": "Smallville High",
"date_range": {
"start": "2001-09-01",
"end": "2005-06-01"
},
"display": "Smallville High (2001-2005)"
}
],
"relationships": [
{
"@type": "family",
"@subtype": "Adoptive Father",
"names": [
{
"first": "Jonathan",
"last": "Kent",
"display": "Jonathan Kent"
}
],
"emails": [
{
"@disposable": true,
"@email_provider": false,
"address": "jkent@example.com",
"address_md5": "e81b8844517b6ab307a9e0fdf973ae3a"
}
]
},
{
"@type": "family",
"@subtype": "Father",
"names": [
{
"first": "Jor",
"last": "El",
"display": "Jor El"
}
]
},
{
"@type": "family",
"@subtype": "Cousin",
"names": [
{
"first": "Kara",
"last": "Kent",
"display": "Kara Kent"
}
]
},
{
"@type": "other",
"@subtype": "Archenemy",
"names": [
{
"first": "Alexander",
"middle": "Joseph",
"last": "Luthor",
"display": "Alexander Joseph Luthor"
}
]
},
{
"@type": "work",
"@subtype": "Colleague",
"names": [
{
"first": "Ron",
"last": "Troupe",
"display": "Ron Troupe"
}
]
},
{
"@type": "friend",
"@subtype": "The real love",
"names": [
{
"first": "Chloe",
"middle": "Anne",
"last": "Sullivan",
"display": "Chloe Anne Sullivan"
}
]
}
],
"user_ids": [
{
"content": "11231@facebook"
}
],
"images": [
{
"url": "http://vignette1.wikia.nocookie.net/smallville/images/e/ea/Buddies_forever.jpg"
},
{
"url": "http://vignette3.wikia.nocookie.net/smallville/images/5/55/S10E18-Booster21.jpg"
}
],
"urls": [
{
"@source_id": "edc6aa8fa3f211cfad7c12a0ba5b32f4",
"@domain": "linkedin.com",
"@name": "LinkedIn",
"@category": "professional_and_business",
"url": "http://linkedin.com/clark.kent"
},
{
"@source_id": "5d836a4acc55922e49fc709c7a39e233",
"@domain": "facebook.com",
"@name": "Facebook",
"@category": "personal_profiles",
"url": "http://facebook.com/superman"
},
{
"@domain": "linkedin.com",
"@category": "professional_and_business",
"url": "https://www.linkedin.com/pub/superman/20/7a/365"
}
]
}
}
但是下面returns一个错误
print("response.raw_json")
print(response.raw_json)
print("")
print("response.person")
print(response.person)
print("")
print("response.raw_json.person.names")
print(response.raw_json.person.names)
错误:
response.person
<piplapis.data.containers.Person object at 0x11263c810>
response.person.names
[Name(first=u'Kal', last=u'El'), Name(first=u'Clark', middle=u'Joseph', last=u'Kent'), Name()]
response.raw_json.person.names
Traceback (most recent call last):
File "InfoFromMails.py", line 34, in <module>
print(response.raw_json.person.names)
AttributeError: 'unicode' object has no attribute 'person'
对我来说 response.raw_json
是一个 JSON 对象,有谁知道为什么会出现 AttributeError?
response.raw_json
是 编码的 JSON 文档 ,Unicode 字符串中的序列化,因此 text。它是不是一个Python数据结构,你必须先把它从JSON解码成一个Python数据结构,这会给你字典和列表:
import json
# ...
response_data = json.loads(response.raw_json)
print(response_data['person']['names']
异常就是这么告诉你的; response.raw_json
是一个 unicode
对象,unicode
对象没有 .person
属性。
因为response_data
是一个字典,你必须使用subscriptions,而不是属性访问。
JSON 主要是 JavaScript 的一个子集,JavaScript 是一种与 Python 有共同词根的语言,因此结构可能 看起来像 像 Python 但非常不是。如果您知道其中的差异,您可以更容易地发现 JSON:
- Python对于字符串的表示输出优先使用单引号(当值中包含单引号时使用双引号),但是JSON只支持双引号。
- JSON 字符串实际上是基于 UTF-16 的,并且 Basic Multilingual Plane are represented as UTF-16 surrogate pairs 之外的代码点使用
\udhhh\dhhh
表示法,其中 Python Unicode 字符串将使用 \U0010hhhh
这些的转义符号。
- Python 布尔值使用首字母大写(
True
、False
),JSON 符号使用小写(true
、false
) .
- Python 有一个
None
单例对象,通常用于指示缺少值,JSON 有一个 null
对象。
- 在 Python 字典上使用
print()
也不会漂亮地打印带有缩进的对象,您必须使用 pprint
库来生成类似的输出。
这里确实不需要再次手动解码原始 JSON 文档,因为 pipl API 库已经为您完成了这项工作并将其解析为容器对象。当您可以只使用 response.person.names
.
时,将 JSON 文档加载到 Python 词典和列表中并没有真正的优势
您的 response.raw_json
变量包含字符串而不是对象。
>>> type(response.raw_json)
<class 'str'>
因此,您需要将其转换为代表 JSON:
的 python 对象
import json
data = json.loads(response.raw_json)
现在您可以通过索引访问您的数据。请记住,有些数据由 name
(对于 dictionaries
)引用,有些数据由 index
(对于 list
s)引用:
first = data['person']['names'][0]['first']
last = data['person']['names'][0]['last']
print("Hello " + first + " " + last)
我正在尝试使用 pipl。我想从邮件中获取信息并将其保存到 Excel 文件中。
from piplapis.search import SearchAPIRequest
from piplapis.search import SearchAPIError
from openpyxl import load_workbook
request = SearchAPIRequest(email=u'clark.kent@example.com', api_key='......')
try:
response = request.send()
except SearchAPIError as e:
print e.http_status_code, e
filename = "mails.xlsx"
wb = load_workbook(filename = filename)
ws = wb['Sheet1']
print(response.raw_json)
returns
{
"@http_status_code": 200,
"@visible_sources": 3,
"@available_sources": 3,
"@persons_count": 1,
"@search_id": "0",
"query": {
"emails": [
{
"address": "clark.kent@example.com",
"address_md5": "2610ee49440fe757e3cc4e46e5b40819"
}
]
},
"available_data": {
"premium": {
"relationships": 6,
"usernames": 2,
"jobs": 3,
"addresses": 2,
"ethnicities": 3,
"phones": 1,
"landline_phones": 1,
"educations": 2,
"languages": 1,
"user_ids": 1,
"social_profiles": 3,
"names": 3,
"dobs": 1,
"images": 2,
"genders": 1,
"emails": 4,
"origin_countries": 1
}
},
"person": {
"@id": "bdc8d748-d7a6-454f-adc0-3b3bd0dd5541",
"@match": 1.0,
"@search_pointer": "1b6b0e83e0c1454d7ba38f7aa219162fcefc67a75ae7397ae44e5b99f159997ea310810c0072a53863abfe151d1885bd5774191ee8cb67aec7877b50d30954c68aaf38129d27caa48562bb006b6be824035d6b13e96be99999d39afe6d758a9b5b212c69eac661d7d59209fe80a61854b5ce40e759c96947bfbce63ae9fef6f5eb8a4cde91d717eeb4d8b888db3ac532a5dea8983c12a5d75f57883b5897f25ed1b66ea17e0e733a020e9f35c02bfb98d724d34c18cae07edca937895cecd534b373c764fd385df7a94f72d08c67ba00a50789bec39986676bb25349f954557ce6e7f69de53984f7cf3e7834444739302998418bf16a70cdee9967bd89e8153bf9fc4f1d5f468e1967f7e17601b1f61d9cb497720addf7e7d1406c03c00af42bfa695d209d831203f812c5a9189f9d45cb75912083182df8b0aab3442b050de5c9fc97aabeb25cd43384c6cdf1af1b160c0d829322a3c9430dbf676c541e00212d484bca4f6b364576f08eea5b97ffe6ea7f991098ba1fbe1ae4f2dad46e7ea239076ba9edd333f9562018bef7fe4e6a967a57dfdd19143e4952f2bae5d889211e25ca7292929e83a68dcb677fb4919297f5a53bac6dc9a20d0f7dff88b1323c45ccf7ac79fb9733a1b2e86fcb8fa75d7b788c1c31462feed9c2d3cc4ec6dc72aca57cfb867fca178c668c00b83ed4d45545c424b3ab97d641593c6d87ac7d595bcf7b028b7d315938e9c0f9dcae7ff799c195b08f2464d3f7222bf1c832cabe2998ad1745db8e59a101b26525dac4b56c7b7ea0b33f47814e87a941266a8e136026064ceb196ebe63c6f3352dfe06ddcef028528c9107d276e5f24c47dd6625217545f3564be287e9047122017b0bb2ecefc905b1e049ec6160d824b2ca6edf52ed4cc4b7a3a7665cb45c28b52c72ef24d36b0c1c6974911fb4bb704682e415094294962eaffca26aa61a8469e5815b02253e54f2f5ddd68eada74fcef090d24d7f491126e6fcfec85345a42e7cbb4b22222c89cc04aa94b530a57c35202ffa9c3fd341f19cfe41c4f47e3c541d83f855945ae2cec1966f9c2681eed7be6270bbe902333d8cecf8a805b26eb8e9dd9a0d7c569a82875918add85d9ed5906d8cf0048ea1912df8bf29eed3a851160b13ba8e87b2ca0b119d3351e1bfc44fe67ab440b236e54b1c2d4bc00ca5856803b85caa708d9337d0add706986653c04e9d56a242c6237bb118397ec1b28ed2cac7d3e3019acbdbd2b8ca1a1aae803c959c5ee57e7f3ef30394d62077ecfc7baffdff5cafe0f4da9037490097a45225419a85fcdd6d3a4fc364ac59df4298a22553db8e0dd1c4e9fa628b40fb409ed6c1d60bf403913644f6d5c57d70737f623a45afd4ed533010585f5208338dc26d560369e6843082ab941e7024ecf5908f458d6478d5745ebde8e60ed5ead82e98f38b302157c8316edbaf4572739a21461cb30d791b874c7b56bdba1f0d5ddc3d7902018336dfe7cc9d364d234395b4cf9dc74a7fbc929d7108d43c27b482d1bafea99277476be0c1933ff30f46a498184b7f2d9da6196b63a427ef14c9e1182fbea0e608988d89129d9b7a0b6094bd39a3ec",
"names": [
{
"first": "Kal",
"last": "El",
"display": "Kal El"
},
{
"first": "Clark",
"middle": "Joseph",
"last": "Kent",
"display": "Clark Joseph Kent"
},
{
"display": "The red blue blur"
}
],
"emails": [
{
"@type": "work",
"@email_provider": false,
"address": "clark.kent@thedailyplanet.com",
"address_md5": "eb3e11de3c9cefc2d9d70972350e2b28"
},
{
"@disposable": true,
"@email_provider": false,
"address": "ck242@guerrillamail.com",
"address_md5": "999e509752141a0ee42ff455529c10fc"
},
{
"@type": "personal",
"@email_provider": true,
"address": "clark@gmail.com",
"address_md5": "501548362894b9a08f071b1565d8aa14"
},
{
"@disposable": true,
"@email_provider": false,
"address": "clark.kent@example.com",
"address_md5": "2610ee49440fe757e3cc4e46e5b40819"
}
],
"usernames": [
{
"content": "superman@facebook"
},
{
"content": "@ckent"
}
],
"phones": [
{
"@type": "home_phone",
"country_code": 1,
"number": 9785550145,
"display": "978-555-0145",
"display_international": "+1 978-555-0145"
}
],
"gender": {
"content": "male"
},
"dob": {
"date_range": {
"start": "1986-01-01",
"end": "1987-05-13"
},
"display": "32 years old"
},
"languages": [
{
"region": "US",
"language": "en",
"display": "en_US"
}
],
"ethnicities": [
{
"content": "other"
},
{
"content": "american_indian"
},
{
"content": "white"
}
],
"origin_countries": [
{
"country": "US"
}
],
"addresses": [
{
"@valid_since": "2005-02-12",
"@type": "work",
"country": "US",
"state": "KS",
"city": "Metropolis",
"street": "Broadway",
"house": "1000",
"apartment": "355",
"display": "1000-355 Broadway, Metropolis, Kansas"
},
{
"@valid_since": "1999-02-01",
"@type": "home",
"country": "US",
"state": "KS",
"city": "Smallville",
"street": "Hickory Lane",
"house": "10",
"apartment": "1",
"zip_code": "66605",
"display": "10-1 Hickory Lane, Smallville, Kansas"
}
],
"jobs": [
{
"title": "Field Reporter",
"organization": "The Daily Planet",
"industry": "Journalism",
"date_range": {
"start": "2000-12-08",
"end": "2012-10-09"
},
"display": "Field Reporter at The Daily Planet (2000-2012)"
},
{
"title": "Junior Reporter",
"organization": "The Daily Planet",
"industry": "Journalism",
"date_range": {
"start": "1999-10-10",
"end": "2000-10-10"
},
"display": "Junior Reporter at The Daily Planet (1999-2000)"
},
{
"title": "Top Reporter",
"organization": "The Daily Planet",
"industry": "Reporting",
"display": "Top Reporter at The Daily Planet"
}
],
"educations": [
{
"degree": "B.Sc Advanced Science",
"school": "Metropolis University",
"date_range": {
"start": "2005-09-01",
"end": "2008-05-14"
},
"display": "B.Sc Advanced Science from Metropolis University (2005-2008)"
},
{
"school": "Smallville High",
"date_range": {
"start": "2001-09-01",
"end": "2005-06-01"
},
"display": "Smallville High (2001-2005)"
}
],
"relationships": [
{
"@type": "family",
"@subtype": "Adoptive Father",
"names": [
{
"first": "Jonathan",
"last": "Kent",
"display": "Jonathan Kent"
}
],
"emails": [
{
"@disposable": true,
"@email_provider": false,
"address": "jkent@example.com",
"address_md5": "e81b8844517b6ab307a9e0fdf973ae3a"
}
]
},
{
"@type": "family",
"@subtype": "Father",
"names": [
{
"first": "Jor",
"last": "El",
"display": "Jor El"
}
]
},
{
"@type": "family",
"@subtype": "Cousin",
"names": [
{
"first": "Kara",
"last": "Kent",
"display": "Kara Kent"
}
]
},
{
"@type": "other",
"@subtype": "Archenemy",
"names": [
{
"first": "Alexander",
"middle": "Joseph",
"last": "Luthor",
"display": "Alexander Joseph Luthor"
}
]
},
{
"@type": "work",
"@subtype": "Colleague",
"names": [
{
"first": "Ron",
"last": "Troupe",
"display": "Ron Troupe"
}
]
},
{
"@type": "friend",
"@subtype": "The real love",
"names": [
{
"first": "Chloe",
"middle": "Anne",
"last": "Sullivan",
"display": "Chloe Anne Sullivan"
}
]
}
],
"user_ids": [
{
"content": "11231@facebook"
}
],
"images": [
{
"url": "http://vignette1.wikia.nocookie.net/smallville/images/e/ea/Buddies_forever.jpg"
},
{
"url": "http://vignette3.wikia.nocookie.net/smallville/images/5/55/S10E18-Booster21.jpg"
}
],
"urls": [
{
"@source_id": "edc6aa8fa3f211cfad7c12a0ba5b32f4",
"@domain": "linkedin.com",
"@name": "LinkedIn",
"@category": "professional_and_business",
"url": "http://linkedin.com/clark.kent"
},
{
"@source_id": "5d836a4acc55922e49fc709c7a39e233",
"@domain": "facebook.com",
"@name": "Facebook",
"@category": "personal_profiles",
"url": "http://facebook.com/superman"
},
{
"@domain": "linkedin.com",
"@category": "professional_and_business",
"url": "https://www.linkedin.com/pub/superman/20/7a/365"
}
]
}
}
但是下面returns一个错误
print("response.raw_json")
print(response.raw_json)
print("")
print("response.person")
print(response.person)
print("")
print("response.raw_json.person.names")
print(response.raw_json.person.names)
错误:
response.person
<piplapis.data.containers.Person object at 0x11263c810>
response.person.names
[Name(first=u'Kal', last=u'El'), Name(first=u'Clark', middle=u'Joseph', last=u'Kent'), Name()]
response.raw_json.person.names
Traceback (most recent call last):
File "InfoFromMails.py", line 34, in <module>
print(response.raw_json.person.names)
AttributeError: 'unicode' object has no attribute 'person'
对我来说 response.raw_json
是一个 JSON 对象,有谁知道为什么会出现 AttributeError?
response.raw_json
是 编码的 JSON 文档 ,Unicode 字符串中的序列化,因此 text。它是不是一个Python数据结构,你必须先把它从JSON解码成一个Python数据结构,这会给你字典和列表:
import json
# ...
response_data = json.loads(response.raw_json)
print(response_data['person']['names']
异常就是这么告诉你的; response.raw_json
是一个 unicode
对象,unicode
对象没有 .person
属性。
因为response_data
是一个字典,你必须使用subscriptions,而不是属性访问。
JSON 主要是 JavaScript 的一个子集,JavaScript 是一种与 Python 有共同词根的语言,因此结构可能 看起来像 像 Python 但非常不是。如果您知道其中的差异,您可以更容易地发现 JSON:
- Python对于字符串的表示输出优先使用单引号(当值中包含单引号时使用双引号),但是JSON只支持双引号。
- JSON 字符串实际上是基于 UTF-16 的,并且 Basic Multilingual Plane are represented as UTF-16 surrogate pairs 之外的代码点使用
\udhhh\dhhh
表示法,其中 Python Unicode 字符串将使用\U0010hhhh
这些的转义符号。 - Python 布尔值使用首字母大写(
True
、False
),JSON 符号使用小写(true
、false
) . - Python 有一个
None
单例对象,通常用于指示缺少值,JSON 有一个null
对象。 - 在 Python 字典上使用
print()
也不会漂亮地打印带有缩进的对象,您必须使用pprint
库来生成类似的输出。
这里确实不需要再次手动解码原始 JSON 文档,因为 pipl API 库已经为您完成了这项工作并将其解析为容器对象。当您可以只使用 response.person.names
.
您的 response.raw_json
变量包含字符串而不是对象。
>>> type(response.raw_json)
<class 'str'>
因此,您需要将其转换为代表 JSON:
的 python 对象import json
data = json.loads(response.raw_json)
现在您可以通过索引访问您的数据。请记住,有些数据由 name
(对于 dictionaries
)引用,有些数据由 index
(对于 list
s)引用:
first = data['person']['names'][0]['first']
last = data['person']['names'][0]['last']
print("Hello " + first + " " + last)