简体   繁体   中英

Bing API Web Search Python

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM