簡體   English   中英

如何通過restful api循環查詢列表?

[英]How to loop a list of queries via restful api?

我正在嘗試顯示來自其余 api 請求的每個水果總提及數。

對 python 非常陌生,所以想知道運行它的最佳方法。

在參數下面,例如,如果我對“ apple ”進行硬編碼,則能夠成功獲得“ apple ”被提及的總次數。

現在我正在嘗試遍歷水果的“查詢”列表,並希望顯示每個水果的總數。

不確定這樣做的最佳途徑。 嘗試在下面運行 for 循環並將其添加到查詢參數中。 我需要在整個 URL 請求上運行一個循環嗎?

import requests

query = ["apple", "orange", "banana", "cherry", "blueberry"]


def mentions():
    for d in query:
        return d


url = 'https://api.fruits.com/Search?'
parameters = {
    "key": "1234567890abc",
    "rt": "json",
    "mode": "full",
    "query": mentions()
}

response = requests.get(url, params=parameters)
data = response.json()

print(data['response']['TotalFound'])

嘗試這個:

import requests

query = ["apple", "orange", "banana", "cherry", "blueberry"]


url = 'https://api.fruits.com/Search?'

data_list = []

for item in query:
    parameters = {
        "key": "1234567890abc",
        "rt": "json",
        "mode": "full",
        "query": item
    }
    response = requests.get(url, params=parameters)
    data = response.json()
    data_list.append(data['response']['TotalFound'])

print(data_list)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM