-4

I'm trying to use pipl. I want to get the info from a mail and save it into a Excel file.

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"
            }
        ]
    }
}

But the following returns an error

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)

The error:

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'

For me response.raw_json is a JSON object, does anyone know why there is an AttributeError?

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
SoftTimur
  • 8,904
  • 23
  • 106
  • 212

2 Answers2

5

response.raw_json is an encoded JSON document, a serialisation in a Unicode string, so text. It is not a Python data structure, you'd have to decode it from JSON into a Python data structure first, which will give you dictionaries and lists:

import json

# ...

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

The exception is telling you exactly that; response.raw_json is a unicode object, and unicode objects don't have a .person attribute.

Because response_data is a dictionary, you have to use subscriptions, not attribute access.

JSON is largely a subset of JavaScript, a language that has a common root with Python, so the structure may look like Python but very much is not. You can spot JSON more easily if you are aware of the differences:

  • Python representation output for strings prefers single quotes (double quotes are used when the value contains single quotes), but JSON only supports double quotes.
  • JSON strings are really UTF-16 based and codepoints outside of the Basic Multilingual Plane are represented as UTF-16 surrogate pairs with \udhhh\dhhh notation, where Python Unicode strings would use the \U0010hhhh escape notation for these.
  • Python booleans use title-case (True, False), JSON notation uses lowercase (true, false).
  • Python has a None singleton object often used to indicate the absence of a value, JSON has a null object instead.
  • Using print() on a Python dictionary won't pretty-print the object with indentation either, you'd have to use the pprint library to produce similar output.

There really is no need to manually decode the raw JSON document again here, as the pipl API library has already done this for you and parsed this out into container objects. There is no real advantage to loading the JSON document into Python dictionaries and lists here, when you could just use response.person.names.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
4

Your response.raw_json variable contains string not an object.

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

So, you need to convert it to the python object which represent JSON:

import json

data = json.loads(response.raw_json)

Now you can access your data via indexing. Remember that some data are referenced by name (for dictionaries) and some by index (for lists):

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

print("Hello " + first + " " + last)
A. Albershteyn
  • 519
  • 3
  • 9