Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract value from a list
#1
I am using an api from a site called dataforseo and I get this result in python (the code is: print(response) ) :

{'version': '0.1.20220819', 'status_code': 20000, 'status_message': 'Ok.', 'time': '5.0801 sec.', 'cost': 0.002, 'tasks_count': 1, 'tasks_error': 0, 'tasks': [{'id': '11291356-4699-0121-0000-2bc31595cb24', 'status_code': 20000, 'status_message': 'Ok.', 'time': '5.0262 sec.', 'cost': 0.002, 'result_count': 1, 'path': ['v3', 'serp', 'google', 'organic', 'live', 'regular'], 'data': {'api': 'serp', 'function': 'live', 'se': 'google', 'se_type': 'organic', 'language_code': 'en', 'location_code': 2840, 'depth': 10, 'keyword': 'albert', 'device': 'desktop', 'os': 'windows'}, 'result': [{'keyword': 'albert', 'type': 'organic', 'se_domain': 'google.com', 'location_code': 2840, 'language_code': 'en', 'check_url': 'https://www.google.com/search?q=albert&num=10&hl=en&gl=US&gws_rd=cr&ie=UTF-8&oe=UTF-8&uule=w+CAIQIFISCQs2MuSEtepUEUK33kOSuTsc', 'datetime': '2022-11-29 11:56:47 +00:00', 'spell': None, 'item_types': ['organic', 'people_also_ask', 'top_stories', 'related_searches', 'knowledge_graph'], 'se_results_count': 1120000000, 'items_count': 8, 'items': [{'type': 'organic', 'rank_group': 1, 'rank_absolute': 1, 'domain': 'www.albert.io', 'title': 'Albert.io', 'description': 'Albert gives teachers access to world-class, standards aligned practice content for AP, Common Core, NGSS, SAT, ACT, Regents, SSTAAR and more.', 'url': 'https://www.albert.io/', 'breadcrumb': 'https://www.albert.io'}, {'type': 'organic', 'rank_group': 2, 'rank_absolute': 2, 'domain': 'albert.com', 'title': 'Albert', 'description': 'Albert is a new type of financial service that uses powerful technology to automate your finances, with a team of human experts to guide you.', 'url': 'https://albert.com/', 'breadcrumb': 'https://albert.com'}, {'type': 'organic', 'rank_group': 3, 'rank_absolute': 3, 'domain': 'albert.nyu.edu', 'title': 'Albert Login', 'description': 'Sign in below to access the Albert portal, or search the public course catalog (no sign-in required). Sign in to Albert · Public Course Search · Albert Help.', 'url': 'https://albert.nyu.edu/', 'breadcrumb': 'https://albert.nyu.edu'}, {'type': 'organic', 'rank_group': 4, 'rank_absolute': 5, 'domain': 'play.google.com', 'title': 'Albert: Banking on you - Apps on Google Play', 'description': "WE'RE BANKING ON YOU At Albert, we want you to win. So we built a better banking, saving, investing, and budgeting solution — with a Genius team of money\xa0...", 'url': 'https://play.google.com/store/apps/details?id=com.meetalbert&hl=en_US&gl=US', 'breadcrumb': 'https://play.google.com › store › apps › details › id=co...'}, {'type': 'organic', 'rank_group': 5, 'rank_absolute': 6, 'domain': 'www.thealbertatlanta.com', 'title': 'The Albert Atlanta', 'description': "The Albert won two awards on the Nextdoor app. We were voted Inman Park's favorite burger and favorite bar/pub in 2017.", 'url': 'https://www.thealbertatlanta.com/', 'breadcrumb': 'https://www.thealbertatlanta.com'}, {'type': 'organic', 'rank_group': 6, 'rank_absolute': 7, 'domain': 'en.wikipedia.org', 'title': 'Albert Einstein - Wikipedia', 'description': 'Albert Einstein was a German-born theoretical physicist, widely acknowledged to be one of the greatest and most influential physicists of all time.', 'url': 'https://en.wikipedia.org/wiki/Albert_Einstein', 'breadcrumb': 'https://en.wikipedia.org › wiki › Albert_Einstein'}, {'type': 'organic', 'rank_group': 7, 'rank_absolute': 8, 'domain': 'thealbertchicago.com', 'title': 'the Albert Chicago: Contemporary Restaurant in Chicago', 'description': 'the Albert is a contemporary restaurant in Chicago that embodies the spirit of the restlessly curious and unconventional.', 'url': 'https://thealbertchicago.com/', 'breadcrumb': 'https://thealbertchicago.com'}, {'type': 'organic', 'rank_group': 8, 'rank_absolute': 10, 'domain': 'www.nobelprize.org', 'title': 'Albert Einstein – Biographical - NobelPrize.org', 'description': '* Albert Einstein was formally associated with the Institute for Advanced Study located in Princeton, New Jersey. Copyright © The Nobel Foundation 1922. To cite\xa0...', 'url': 'https://www.nobelprize.org/prizes/physics/1921/einstein/biographical/', 'breadcrumb': 'https://www.nobelprize.org › prizes › physics › biograp...'}]}]}]}
I would like to get the value of 'url'. There are 8 urls here.
How can I do that?
Reply
#2
the_dict = {'version': '0.1.20220819', 'status_code': 20000, 'status_message': 'Ok.', 'time': '5.0801 sec.', 'cost': 0.002, 'tasks_count': 1, 'tasks_error': 0, 'tasks': [{'id': '11291356-4699-0121-0000-2bc31595cb24', 'status_code': 20000, 'status_message': 'Ok.', 'time': '5.0262 sec.', 'cost': 0.002, 'result_count': 1, 'path': ['v3', 'serp', 'google', 'organic', 'live', 'regular'], 'data': {'api': 'serp', 'function': 'live', 'se': 'google', 'se_type': 'organic', 'language_code': 'en', 'location_code': 2840, 'depth': 10, 'keyword': 'albert', 'device': 'desktop', 'os': 'windows'}, 'result': [{'keyword': 'albert', 'type': 'organic', 'se_domain': 'google.com', 'location_code': 2840, 'language_code': 'en', 'check_url': 'https://www.google.com/search?q=albert&num=10&hl=en&gl=US&gws_rd=cr&ie=UTF-8&oe=UTF-8&uule=w+CAIQIFISCQs2MuSEtepUEUK33kOSuTsc', 'datetime': '2022-11-29 11:56:47 +00:00', 'spell': None, 'item_types': ['organic', 'people_also_ask', 'top_stories', 'related_searches', 'knowledge_graph'], 'se_results_count': 1120000000, 'items_count': 8, 'items': [{'type': 'organic', 'rank_group': 1, 'rank_absolute': 1, 'domain': 'www.albert.io', 'title': 'Albert.io', 'description': 'Albert gives teachers access to world-class, standards aligned practice content for AP, Common Core, NGSS, SAT, ACT, Regents, SSTAAR and more.', 'url': 'https://www.albert.io/', 'breadcrumb': 'https://www.albert.io'}, {'type': 'organic', 'rank_group': 2, 'rank_absolute': 2, 'domain': 'albert.com', 'title': 'Albert', 'description': 'Albert is a new type of financial service that uses powerful technology to automate your finances, with a team of human experts to guide you.', 'url': 'https://albert.com/', 'breadcrumb': 'https://albert.com'}, {'type': 'organic', 'rank_group': 3, 'rank_absolute': 3, 'domain': 'albert.nyu.edu', 'title': 'Albert Login', 'description': 'Sign in below to access the Albert portal, or search the public course catalog (no sign-in required). Sign in to Albert · Public Course Search · Albert Help.', 'url': 'https://albert.nyu.edu/', 'breadcrumb': 'https://albert.nyu.edu'}, {'type': 'organic', 'rank_group': 4, 'rank_absolute': 5, 'domain': 'play.google.com', 'title': 'Albert: Banking on you - Apps on Google Play', 'description': "WE'RE BANKING ON YOU At Albert, we want you to win. So we built a better banking, saving, investing, and budgeting solution — with a Genius team of money\xa0...", 'url': 'https://play.google.com/store/apps/details?id=com.meetalbert&hl=en_US&gl=US', 'breadcrumb': 'https://play.google.com › store › apps › details › id=co...'}, {'type': 'organic', 'rank_group': 5, 'rank_absolute': 6, 'domain': 'www.thealbertatlanta.com', 'title': 'The Albert Atlanta', 'description': "The Albert won two awards on the Nextdoor app. We were voted Inman Park's favorite burger and favorite bar/pub in 2017.", 'url': 'https://www.thealbertatlanta.com/', 'breadcrumb': 'https://www.thealbertatlanta.com'}, {'type': 'organic', 'rank_group': 6, 'rank_absolute': 7, 'domain': 'en.wikipedia.org', 'title': 'Albert Einstein - Wikipedia', 'description': 'Albert Einstein was a German-born theoretical physicist, widely acknowledged to be one of the greatest and most influential physicists of all time.', 'url': 'https://en.wikipedia.org/wiki/Albert_Einstein', 'breadcrumb': 'https://en.wikipedia.org › wiki › Albert_Einstein'}, {'type': 'organic', 'rank_group': 7, 'rank_absolute': 8, 'domain': 'thealbertchicago.com', 'title': 'the Albert Chicago: Contemporary Restaurant in Chicago', 'description': 'the Albert is a contemporary restaurant in Chicago that embodies the spirit of the restlessly curious and unconventional.', 'url': 'https://thealbertchicago.com/', 'breadcrumb': 'https://thealbertchicago.com'}, {'type': 'organic', 'rank_group': 8, 'rank_absolute': 10, 'domain': 'www.nobelprize.org', 'title': 'Albert Einstein – Biographical - NobelPrize.org', 'description': '* Albert Einstein was formally associated with the Institute for Advanced Study located in Princeton, New Jersey. Copyright © The Nobel Foundation 1922. To cite\xa0...', 'url': 'https://www.nobelprize.org/prizes/physics/1921/einstein/biographical/', 'breadcrumb': 'https://www.nobelprize.org › prizes › physics › biograp...'}]}]}]}

for url in the_dict["tasks"][0]["result"][0]["items"]:
    print(url["url"])
Output:
https://www.albert.io/ https://albert.com/ https://albert.nyu.edu/ https://play.google.com/store/apps/details?id=com.meetalbert&hl=en_US&gl=US https://www.thealbertatlanta.com/ https://en.wikipedia.org/wiki/Albert_Einstein https://thealbertchicago.com/ https://www.nobelprize.org/prizes/physics/1921/einstein/biographical/
thesquid likes this post
Reply
#3
Oh thank you so much. I would have never found the solution myself. I wasted 2 hours already :)
It's working.

I am analyzing the code to learn from it. Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Need help for a python script to extract information from a list of files lephunghien 6 1,111 Jun-12-2023, 05:40 PM
Last Post: snippsat
  extract first and last 5 elements from given list and generate a new list. Raj_Kumar 1 2,383 Dec-07-2019, 05:03 PM
Last Post: ichabod801
  Extract email addresses from string and store them in a list oslosurfer 2 2,719 Nov-24-2019, 03:35 PM
Last Post: oslosurfer
  python3 List to array or string to extract data batchenr 4 3,284 May-28-2019, 01:44 PM
Last Post: buran
  Loop files - Extract List Data To Individual Columns in CSV dj99 5 3,295 May-19-2019, 10:29 AM
Last Post: dj99
  Extract a List element NewBeie 4 2,921 May-03-2019, 09:49 AM
Last Post: NewBeie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020