2014-01-29 109 views
0

我有一個簡單的腳本使用請求來驗證電子郵件列表。相關代碼:AttributeError:'unicode'對象沒有'成功'屬性

def ___process_email(email, output_file=None): 
    profile = request(email) 
    if profile and profile.success != 'nothing_useful': 
     logger.info('Found match for {0}'.format(email)) 
     print(profile) 
     if output_file: 
      output_file.write(str(profile) + '\n') 
    else: 
     print("No information found\n") 

這通過5圈跑成功再摔:

Traceback (most recent call last): 
    File "app.py", line 147, in <module> main() 
    File "app.py", line 141, in main ___process_email(arg, output) 
    File "app.py", line 107, in ___process_email if profile and profile.success != 'nothing_useful': 
AttributeError: 'unicode' object has no attribute 'success' 

這裏的模型:

class Profile(object): 
    def __init__(self, person): 
     if person: 
      self.name = person.get('name') 
      self.jobinfo = [ 
       (occupation.get('job_title'), occupation.get('company')) 
       for occupation in person.get('occupations', []) 
      ] 

      self.memberships = [ 
       (membership.get('site_name'), membership.get('profile_url')) 
       for membership in person.get('memberships', []) 
      ] 
      self.success = person.get('success') 

    def __str__(self): 
     return dedent(""" 
      Name: {0} 
      {1} 
      {2} 
     """).format(
      self.name, 
      "\n".join(
       "{0} {1}".format(title, company) 
       for title, company in self.jobinfo), 
      "\n".join(
       "\t{0} {1}".format(site_name, url) 
       for site_name, url in self.memberships) 
     ) 

請求:

import requests 

def request(email): 

    status_url = STATUS_URL.format(email) 
    response = requests.get(status_url).json() 
    session_token = response.get('session_token') 
    # fail gracefully if there is an error 
    if 'error' in response: 
     return response['error'] 
    elif response['status'] == 200 and session_token: 
     logger.debug('Session token: {0}'.format(session_token)) 
     url = URL.format(email) 
     headers = {'X-Session-Token': session_token} 
     response = requests.get(url, headers=headers).json() 
     if response.get('success') != 'nothing_useful': 
      return Profile(response.get('contact')) 
    return {} 

任何人都明白爲什麼我的字符串是unicode?感謝

+0

什麼是'request' h ERE? –

+0

感謝更新以顯示請求 –

+0

是否將'email'作爲URL參數傳遞給後端服務?也許你應該在這種情況下將GET參數添加到'requests.get()'函數中。 –

回答

3

如果在響應中出現錯誤,將返回錯誤字符串:

if 'error' in response: 
    return response['error'] 

這就是你的unicode價值存在。請注意,相同的函數返回'error'值,新的Profile()實例或空字典。您可能希望使其更加一致,只返回Profile()座位和None

取而代之的是錯誤的字符串,引發一個異常和處理異常的___process_email方法:

class EmailValidationError(Exception): 
    pass 

,並在您request()功能:

if 'error' in response: 
    raise EmailValidationError(response['error']) 

然後用像處理這__process_email()

try: 
    profile = request(email) 
    if profile and profile.success != 'nothing_useful': 
     logger.info('Found match for {0}'.format(email)) 
     print(profile) 
     if output_file: 
      output_file.write(str(profile) + '\n') 
    else: 
     print("No information found\n") 
except EmailValidationError: 
    # Do something here