0

I'm trying to create a search bot which uses Bing Web Search API but I'm facing an issue.

subscription_key = API_KEY
assert subscription_key
search_url = "https://api.cognitive.microsoft.com/bing/v7.0/search"
search_term = "Sayam Kanwar"

import requests

headers = {"Ocp-Apim-Subscription-Key" : subscription_key}
params  = {"q": search_term, "textDecorations":True, "textFormat":"HTML"}
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json()

print search_results["webPages"]["value"]

Output:

Screenshot

Now, I want to extract just the u'name' from all of them and create a separate array which would contain all the u'name's.

Please help me out.

Thanks!

sayamkanwar
  • 49
  • 1
  • 10

1 Answers1

0

By the looks of it we seem to have an array of dictionaries in your result (like [{'name': 'foo'},{'name': 'bar'}] you can ignore the u for the most part. It just says that that string is Unicode).

One option to extract all the names would be to loop through the list and for every dictionary append the name in it to another array.

Bob
  • 614
  • 1
  • 7
  • 19
  • Can you please provide me the code? I'm having a bit difficulty interpreting your suggestion. – sayamkanwar Feb 21 '18 at 20:08
  • Sort of like this: `names = [] for person in search_results["webPages"]["value"]: names.append(person["name"])` – Bob Feb 21 '18 at 20:08