Iterable is returning the last value for all elements in the list

45 Views Asked by At

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.

1

There are 1 best solutions below

0
Dmitry On

The team is never used. Supposed to be in request parameters:

parameters = {'q': team, 'limit': number_of_results}

And there are few other enhancements:

  • The result of url construction stays the same on each iteration of loop - should be built once and outside of the loop context;
  • f-strings are better readable than string concatenation;

Concerning these, the snippet becomes:

import requests

base_url = 'https://api.wikimedia.org/core/v1/wikipedia/'
language_code = 'es'
endpoint = '/search/page'
url = f"{base_url}{language_code}{endpoint}"

number_of_results = 1
headers = {
      'User-Agent': 'YOUR_APP_NAME (YOUR_EMAIL_OR_CONTACT_PAGE)'
}

teams = ['Esteghlal FC', 'Liverpool FC']
articles = []
for team in teams:
    parameters = {'q': team, 'limit': number_of_results}
    response = requests.get(url, headers=headers, params=parameters)
    articles.append(response.json())

The result content of articles is:

[{'pages': [{'id': 3022896,
    'key': 'Esteghlal_Tehran_Football_Club',
    'title': 'Esteghlal Tehran Football Club',
    'excerpt': 'El Club Cultural y Atlético...',
    'matched_title': None,
    'description': 'club Cultural y Atlético Esteghlal',
    'thumbnail': None}]},
 {'pages': [{'id': 138534,
    'key': 'Liverpool_Football_Club',
    'title': 'Liverpool Football Club',
    'excerpt': 'El <span class="searchmatch">Liverpool</span> Football Club...
    '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/...'}}]}]