AttributeError:'unicode'对象没有属性XXX

问题描述 投票:-6回答:2

我正在尝试使用pipl。我想从邮件中获取信息并将其保存到Excel文件中。

from piplapis.search import SearchAPIRequest
from piplapis.search import SearchAPIError
from openpyxl import load_workbook

request = SearchAPIRequest(email=u'[email protected]', 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)回归

{
    "@http_status_code": 200,
    "@visible_sources": 3,
    "@available_sources": 3,
    "@persons_count": 1,
    "@search_id": "0",
    "query": {
        "emails": [
            {
                "address": "[email protected]",
                "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": "[email protected]",
                "address_md5": "eb3e11de3c9cefc2d9d70972350e2b28"
            },
            {
                "@disposable": true,
                "@email_provider": false,
                "address": "[email protected]",
                "address_md5": "999e509752141a0ee42ff455529c10fc"
            },
            {
                "@type": "personal",
                "@email_provider": true,
                "address": "[email protected]",
                "address_md5": "501548362894b9a08f071b1565d8aa14"
            },
            {
                "@disposable": true,
                "@email_provider": false,
                "address": "[email protected]",
                "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": "[email protected]",
                        "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"
            }
        ]
    }
}

但以下返回错误

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?

python json python-2.7 python-unicode
2个回答
2
投票

你的response.raw_json变量包含字符串而不是对象。

>>> type(response.raw_json)
<class 'str'>

因此,您需要将其转换为代表JSON的python对象:

import json

data = json.loads(response.raw_json)

现在,您可以通过索引访问您的数据。请记住,一些数据由name(对于dictionaries)和一些index(对于lists)引用:

first = data['person']['names'][0]['first']
last = data['person']['names'][0]['last']

print("Hello " + first + " " + last)

5
投票

response.raw_json是一个编码的JSON文档,是Unicode字符串中的序列化,因此是文本。它不是Python数据结构,您必须首先将它从JSON解码为Python数据结构,这将为您提供字典和列表:

import json

# ...

response_data = json.loads(response.raw_json)
print(response_data['person']['names']

例外是告诉你的确如此; response.raw_jsonunicode对象,unicode对象没有.person属性。

因为response_data是一个字典,你必须使用subscriptions,而不是属性访问。

JSON主要是JavaScript的一个子集,一种与Python有共同根的语言,因此结构可能看起来像Python,但实际上并非如此。如果您意识到差异,则可以更轻松地发现JSON:

  • 字符串的Python表示输出更喜欢单引号(当值包含单引号时使用双引号),但JSON仅支持双引号。
  • JSON字符串实际上是基于UTF-16的,Basic Multilingual Plane之外的代码点用UTF-16 surrogate pairs表示法表示为\udhhh\dhhh,其中Python Unicode字符串将使用\U0010hhhh转义表示法。
  • Python布尔值使用title-case(TrueFalse),JSON表示法使用小写(truefalse)。
  • Python有一个None单例对象,通常用于表示缺少值,JSON有一个null对象。
  • 在Python字典上使用print()也不会使用缩进来打印对象,你必须使用pprint库来产生类似的输出。

实际上没有必要在这里再次手动解码原始JSON文档,因为pipl API库已经为您完成了此操作并将其解析为容器对象。当您可以使用response.person.names时,将JSON文档加载到Python字典和列表中没有任何实际优势。

© www.soinside.com 2019 - 2024. All rights reserved.