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!

1 Answer 1

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.