I'm performing a simple calling to Wikipedia API using "request" as recommended in documentation. However, whereas it works well for a single request, the last response is repeated everytime when I use a list as input.
The code I implemented is the following:
import requests
search_query = ['Esteghlal FC','Liverpool FC',]
language_code = 'es'
number_of_results = 1
headers = {
# 'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
'User-Agent': 'YOUR_APP_NAME (YOUR_EMAIL_OR_CONTACT_PAGE)'
}
articles = []
for team in search_query:
base_url = 'https://api.wikimedia.org/core/v1/wikipedia/'
endpoint = '/search/page'
url = base_url + language_code + endpoint
parameters = {'q': search_query, 'limit': number_of_results}
response = requests.get(url, headers=headers, params=parameters)
articles.append(response.json())
When I print articles,
print(len(articles))
for article in articles:
print(article)
I get this:
2
{'pages': [{'id': 138534, 'key': 'Liverpool_Football_Club', 'title': 'Liverpool Football Club', 'excerpt': 'El <span class="searchmatch">Liverpool</span> Football Club (pronunciación en inglés: /ˈlɪvərpul ˈfʊtbɔl klʌb/) es un club de fútbol profesional inglés con sede en <span class="searchmatch">Liverpool</span>, Inglaterra', 'matched_title': None, 'description': 'club deportivo de Liverpool, Inglaterra', 'thumbnail': {'mimetype': 'image/jpeg', 'width': 60, 'height': 74, 'duration': None, 'url': '//upload.wikimedia.org/wikipedia/commons/thumb/7/75/Liverpool_FC_crest_on_Walton_Breck_Road.jpg/60px-Liverpool_FC_crest_on_Walton_Breck_Road.jpg'}}]}
{'pages': [{'id': 138534, 'key': 'Liverpool_Football_Club', 'title': 'Liverpool Football Club', 'excerpt': 'El <span class="searchmatch">Liverpool</span> Football Club (pronunciación en inglés: /ˈlɪvərpul ˈfʊtbɔl klʌb/) es un club de fútbol profesional inglés con sede en <span class="searchmatch">Liverpool</span>, Inglaterra', 'matched_title': None, 'description': 'club deportivo de Liverpool, Inglaterra', 'thumbnail': {'mimetype': 'image/jpeg', 'width': 60, 'height': 74, 'duration': None, 'url': '//upload.wikimedia.org/wikipedia/commons/thumb/7/75/Liverpool_FC_crest_on_Walton_Breck_Road.jpg/60px-Liverpool_FC_crest_on_Walton_Breck_Road.jpg'}}]}
Does anyone know why thats the case? Appreciate all the help.
The
teamis never used. Supposed to be in request parameters:And there are few other enhancements:
urlconstruction stays the same on each iteration of loop - should be built once and outside of the loop context;Concerning these, the snippet becomes:
The result content of
articlesis: