Wikipedia Summary Python API
Introduction
Imagine you want to get chemical properties of Oxygen. There should be an article about Oxygen on Wikipedia. How about Microsoft corporation ? Who is the CEO ? Where is the headquarter located ? Again, Microsoft is so popular so there must be an article about it on Wikipedia. The good news is that each good quality article on Wikipedia there is a summary table on the right side of the page. In this article I am going to demonstrate how to extract such information easily using few API calls. I am going to use Python but you can use any language you prefer. Let us get started…
RapidAPI
Follow the steps below to access the API.
- Go to https://rapidapi.com/8bitavenue-wikipedia-squad/api/wikiapi
- If you do not have a RapidAPI account register a new account. You just need an email address. It is free and RapidAPI is a legit business so there is no security risk in doing that. RapidAPI is relatively a new company but it is the dominant market place for APIs.
- Click subscribe. Currently there is only one plan which is totally free. No credit card is needed and if you reach the maximum number of requests per month there is a hard limit so nothing is going to be charged.
- In order to subscribe to any plan you need to have a RapidAPI app. If you do not have one click on My Apps on the top right corner and create one. Once you are done you can select that app before subscribing to the API
- At this point you can explore the list of available APIs and test any endpoint from RapidAPI UI. You can even copy and paste code samples using your preferred programming language.
Using the API
For demonstration let us try to extract headline news from Wikipedia. Here is a code snippet to do that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import requests url = "https://wikiapi.p.rapidapi.com/api/v1/wiki/home/news" querystring = {"lan":"en"} headers = { 'x-rapidapi-key': "Get this key from your rapidapi account", 'x-rapidapi-host': "wikiapi.p.rapidapi.com" } response = requests.request("GET", url, headers=headers, params=querystring) print(response.text) |
Note that the language is hardcoded to English (en in this case). To try other languages (if supported) just use the language you want for example ar for Arabic. If everything is ok you should get an output like…
1 2 3 4 5 6 7 |
import requests [ 0:"A coordinated short squeeze targeting American hedge funds causes GameStop's stock price to spike." 1:"Kaja Kallas (pictured) becomes the first female prime minister of Estonia, following the resignation of Jüri Ratas." 2:"In Russia, protests against the arrest of Alexei Navalny occur in more than 120 towns and cities." ] |
Here is another example where we will be extracting information about battle field tanks. First we need to know what tanks the API supports. To do that use the code below…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import requests url = "https://wikiapi.p.rapidapi.com/api/v1/wiki/military/armoured/tank/names" querystring = {"lan":"en"} headers = { 'x-rapidapi-key': "Get this key from your rapidapi account", 'x-rapidapi-host': "wikiapi.p.rapidapi.com" } response = requests.request("GET", url, headers=headers, params=querystring) print(response.text) |
The output will look like…
1 2 3 4 5 6 7 8 |
{ "a_20_tank":"A-20 tank" "a_32_medium_tank":"A-32 medium tank" "al_khalid_tank":"Al-Khalid tank" "al_zarrar":"Al-Zarrar" "altay":"Altay (tank)", "m1_abrams": "M1 Abrams" } |
Now let us get the tank information for the M1 Abrams tank. Use the following code…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import requests url = "https://wikiapi.p.rapidapi.com/api/v1/wiki/military/armoured/tank/info/m1_abrams" querystring = {"lan":"en"} headers = { 'x-rapidapi-key': "Get this key from your rapidapi account", 'x-rapidapi-host': "wikiapi.p.rapidapi.com" } response = requests.request("GET", url, headers=headers, params=querystring) print(response.text) |
If everything is ok you should get an output like…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
{ "armor":"composite armor" "crew":"4 (commander/machine gunner, gunner, loader, driver)" "designed":"1972–1975" "designer":"Chrysler Defense (now General Dynamics Land Systems)" "engine":"Honeywell AGT1500C multi-fuel turbine engine1,500 shp (1,120 kW)" "fuel_capacity":"504.4 US gallons (1,909 l; 420.0 imp gal)" "ground_clearance":"M1, M1A1: 0.48 m (1 ft 7 in) M1A2: 0.43 m (1 ft 5 in)" "height":"8 ft (2.44 m)" "in_service":"1980–present" "length":"Gun forward: 32.04 ft (9.77 m) Hull length: 26.02 ft (7.93 m)" "main_armament":"M1: 105 mm L/52 M68A1 rifled gun (55 rounds) M1A1: 120 mm L/44 M256A1 smoothbore gun (40 rounds) M1A2: 120 mm L/44 M256A1 smoothbore gun (42 rounds)" "manufacturer":"Lima Army Tank Plant (since 1980)[1] , Detroit Arsenal Tank Plant (1982–1996)" "mass":"M1: 60 short tons (54 t) M1A1: 63 short tons (57 t) M1A1 SA: 67.6 short tons (61.3 t) M1A2 SEPv2: 71.2 short tons (64.6 t) M1A2C: 73.6 short tons (66.8 t)" "max_speed":"M1A1: Road 45 mph (72 km/h) (governed); Off-road: 30 mph (48 km/h) M1A2: Road 42 mph (67 km/h) (governed); Off-road: 25 mph (40 km/h)" "name":"M1 Abrams" "num_built":"approx. 10,288" "operational_range":"M1A2 Road: 426 km (265 mi)Cross country: 150–200 km (93–124 mi)" "photo_desc":"U.S. Army M1A2 Abrams with production TUSK explosive reactive armor package installed" "photo_img":"https://upload.wikimedia.org/wikipedia/commons/thumb/9/92/Mounted_Soldier_System_%28MSS%29.jpg/300px-Mounted_Soldier_System_%28MSS%29.jpg" "place_of_origin":"United States" "power_weight":"From 26.9 hp/t (20.05 kW/t) to 23.8 hp/t (17.74 kW/t)" "produced":"1979–present" "secondary_armament":"1 × .50-caliber (12.7 mm) M2HB heavy machine gun with 900 rounds 2 × 7.62 mm (.308 in) M240 machine guns with 10,400 rounds (1 pintle-mounted, 1 coaxial)" "suspension":"High-hardness-steel torsion bars with rotary shock absorbers" "transmission":"Allison DDA X-1100-3B" "type":"Main battle tank" "unit_cost":"US$6.21 million (M1A2 / FY99) Estimated in 2016 as US$8.92 million (with inflation adjustment)" "used_by":"See Operators below" "wars":"Persian Gulf War , War in Afghanistan , Iraq War , Egyptian revolution of 2011 , Iraqi Civil War , Saudi Arabian-led intervention in Yemen" "width":"12 ft (3.66 m)" } |
That is it for today. Just explore the list of available APIs for a topic. Currently there are 45 endpoints supporting various topics such as biography, business, science, geography, sports, education, engineering, military, history and animals. If you have question please leave a comment blow.