0
votes

I am writing basic python script for getting api response of wikipedia api using below code. i am new to python, can anyone point out my mistake please.

import requests
from flask import Flask

URL = 'https://en.wikipedia.org/w/api.php'
r = requests.get(URL).json()
print(r)

Traceback (most recent call last): File "C:/Users/Haider Ghufran/PycharmProjects/scrap_api/web_api.py", line 7, in r = requests.get(URL).json() File "C:\ProgramData\Anaconda3\envs\scrap_api\lib\site-packages\requests\models.py", line 897, in json return complexjson.loads(self.text, **kwargs) File "C:\ProgramData\Anaconda3\envs\scrap_api\lib\json__init__.py", line 348, in loads return _default_decoder.decode(s) File "C:\ProgramData\Anaconda3\envs\scrap_api\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\ProgramData\Anaconda3\envs\scrap_api\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

2

2 Answers

2
votes
import requests
url = 'http://en.wikipedia.org/w/api.php?action=query&prop=info&format=json&titles=Stanford%20University'
resp = requests.get(url)
data = resp.json()

print(data)

This is an example of a query for the api using arguments.

0
votes

I recommend you to start consulting the API help in MediaWiki:

https://www.mediawiki.org/wiki/API:Main_page

By default, the results are not returned in JSON format. For example, here is call sample to Spanish Wikipedia to retrieve the sections of a page:

https://es.wikipedia.org/w/api.php?action=parse&pageid=6903884&prop=sections

and the same one in JSON (adding format=json to the request):

https://es.wikipedia.org/w/api.php?action=parse&pageid=6903884&prop=sections&format=json