python - Not understanding why I'm getting a list index error -
i'm writing code automate of b2b email scraping. have function, hunter.domain_search(url), outputs dictionary lists keys this
{ 'domain': 'turbotitleloan.com', 'webmail': false, 'pattern': none, 'organization': none, 'emails': [ { 'value': 'support@turbotitleloan.com', 'type': 'generic', 'confidence': 6, 'sources': [ { 'domain': 'turbotitleloan.com', 'uri': 'http://turbotitleloan.com/car-title-loans/title-loan-faqs', 'extracted_on': '2016-06-10' }, { 'domain': 'turbotitleloan.com', 'uri': 'http://turbotitleloan.com/contact', 'extracted_on': '2016-06-10' }, { 'domain': 'turbotitleloan.com', 'uri': 'http://turbotitleloan.com/faq', 'extracted_on': '2015-01-30' } ], 'first_name': none, 'last_name': none, 'position': none, 'linkedin': none, 'twitter': none, 'phone_number': none }, { 'value': 'underwriting@turbotitleloan.com', 'type': 'generic', 'confidence': 5, 'sources': [ { 'domain': 'turbotitleloan.com', 'uri': 'http://turbotitleloan.com/faq', 'extracted_on': '2016-05-02' }, { 'domain': 'turbotitleloan.com', 'uri': 'http://turbotitleloan.com/car-title-loans/what-to-expect', 'extracted_on': '2016-06-10' }, { 'domain': 'turbotitleloan.com', 'uri': 'http://turbotitleloan.com/car-title-loans/title-loan-faqs', 'extracted_on': '2016-06-10' } ], 'first_name': none, 'last_name': none, 'position': none, 'linkedin': none, 'twitter': none, 'phone_number': none } ] } and want code return values paired key 'values', series of email addresses. urls list of domain names. here's think issue is
for url in urls: result=hunter.domain_search(url) nresult=result['emails'] nresult[0]['value'] it outputs single email address , list index out of range error. i'm not getting why i'm encountering error. each time goes through loop working new list, , of these lists should have first element, dictionary want access stored. why loop working on first list, , not on subsequent ones?
the index error caused function returning result empty emails list. try skip entries without emails.
for url in urls: result=hunter.domain_search(url) nresult=result['emails'] if nresult: print(nresult[0]['value']) else: print('no emails found in', url)
Comments
Post a Comment