开发者

Python Loop Json Response Returning Empty

开发者 https://www.devze.com 2022-12-07 20:20 出处:网络
I am new in Python, Trying to Extract results from below json response params = { \'api_key\': \'demo\',

I am new in Python, Trying to Extract results from below json response

params = {
  'api_key': 'demo',
  'q': 'pizza'
}

api_result = requests.get('https://api.valueserp.com/search', params) 

above is giving a json response like below

 {
  
   "ads":[...]
   "organic_results":[
       0:{
          "position":1
          "title":"Pizza Hut: Pizza Delivery | Pizza Carryout | Coupons | Wings ..."
          "link":"https://www.pizzahut.com/"
          "domain":"www.pizzahut.com"
          "displayed_link":"https://www.pizzahut.com"
          "snippet":"DELIVERY & CARRYOUT LOCATIONS ARE OPEN! Order pizza online"
          "prerender":false
          "snippet_matched":[...]
          "sitelinks":{...}
          "rich_snippet":{...}
          "about_this_result":{...}
          &qu开发者_开发知识库ot;date":"Apr 12, 2020"
          "date_utc":"2020-04-12T11:00:00.000Z"
          "block_position":3
         }
       1:{
           "position":2
           "title":"Domino's: Pizza Delivery & Carryout, Pasta, Chicken & More"
           "link":"https://www.dominos.com/"
           "domain":"www.dominos.com"
           "displayed_link":"https://www.dominos.com"
           "snippet":"Order pizza, pasta, sandwiches & more online for"
           "prerender":false
           "snippet_matched":[...]
           "sitelinks":{...}
           "about_this_result":{...}
           "rich_snippet":{...}
           "block_position":4
          }
       }

I am trying to loop through the organic_results and written below code for this but it is returning an empty

     def get_serp_comp(results):
       serp_comp = []
       for x in results["organic_results"]:

       serp_comp.append(x)
       return serp_comp

     #call above function
     get_serp_comp(api_result.json())

what is the wrong I am doing and how to loop through the organic_results and print its elements like title, link etc

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号