{"id":26067,"date":"2021-12-14T08:45:37","date_gmt":"2021-12-14T03:15:37","guid":{"rendered":"https:\/\/python-programs.com\/?p=26067"},"modified":"2021-12-14T08:45:57","modified_gmt":"2021-12-14T03:15:57","slug":"python-wikipedia-module-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-wikipedia-module-with-examples\/","title":{"rendered":"Python Wikipedia Module with Examples"},"content":{"rendered":"

Wikipedia Module:<\/strong><\/p>\n

Python’s Wikipedia module can be used to get a large amount of information from the well-known Wikipedia website.<\/p>\n

We will begin by including the Wikipedia module in our system. If the importing command fails to work. Make sure you use the pip command to install the module.<\/p>\n

Installation of wikipedia module:<\/strong><\/p>\n

pip install wikipedia<\/pre>\n

Output:<\/strong><\/p>\n

Collecting wikipedia Downloading wikipedia-1.4.0.tar.gz (27 kB) Requirement \r\nalready satisfied: beautifulsoup4 in \/usr\/local\/lib\/python3.7\/dist-packages \r\n(from wikipedia) (4.6.3) Requirement already satisfied: requests<3.0.0,>=2.0.0 \r\nin \/usr\/local\/lib\/python3.7\/dist-packages (from wikipedia) (2.23.0) Requirement \r\nalready satisfied: idna<3,>=2.5 in \/usr\/local\/lib\/python3.7\/dist-packages \r\n(from requests<3.0.0,>=2.0.0->wikipedia) (2.10) Requirement already satisfied: \r\ncertifi>=2017.4.17 in \/usr\/local\/lib\/python3.7\/dist-packages (from requests<3.0\r\n.0,>=2.0.0->wikipedia) (2021.10.8) Requirement already satisfied: chardet<4,>=3\r\n.0.2 in \/usr\/local\/lib\/python3.7\/dist-packages (from requests<3.0.0,>=2.0.0->\r\nwikipedia) (3.0.4) Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,\r\n<1.26,>=1.21.1 in \/usr\/local\/lib\/python3.7\/dist-packages (from requests<3.0.0,>\r\n=2.0.0->wikipedia) (1.24.3) Building wheels for collected packages: wikipedia \r\nBuilding wheel for wikipedia (setup.py) ... done Created wheel for wikipedia: \r\nfilename=wikipedia-1.4.0-py3-none-any.whl size=11696 sha256=77f5cc37acc194cd3b\r\n7e74d9980194a3ef1a991797e43b89a69bc102dedafc4d Stored in directory: \/root\/.\r\ncache\/pip\/wheels\/15\/93\/6d\/5b2c68b8a64c7a7a04947b4ed6d89fb557dcc6bc27d1d7f3ba \r\nSuccessfully built wikipedia Installing collected packages: wikipedia \r\nSuccessfully installed wikipedia-1.4.0<\/pre>\n

How to get the Data from the Wikipedia module?<\/strong><\/p>\n

To get Random Pages:<\/strong><\/p>\n

Choosing appropriate titles to search for might be a difficult process at times. Using the random method, one can obtain random titles.<\/p>\n

If we require more than one random title, we can pass the number of pages as a parameter to the method. A list of titles is returned by the function.<\/p>\n

# Import wikipedia module using the import keyword.\r\nimport wikipedia \r\n# Pass the number of pages as an argument to the random() function to get some \r\n# random titles and print it\r\nprint(wikipedia.random(pages=8))<\/pre>\n

Output:<\/strong><\/p>\n

['Gabriel P 6', 'Ambulyx charlesi', \"Shooting at the 2014 Asian Games \u2013\r\n Women's 10 metre air pistol team\", 'Favolaschia', 'Akheem Gauntlett', \r\n'Jhorahat', 'Pavan Deshpande', 'Valerio Baldassari']<\/pre>\n

To get the summary:<\/strong><\/p>\n

Any tittle can be summarised using the summary method.<\/p>\n

The summary method accepts a string as an argument that defines the title to search for. It returns a number of sentences for the specified title.<\/p>\n

We can also add the number of sentences required as a parameter to limit the amount of\u00a0stored\u00a0data.<\/p>\n

Approach:<\/strong><\/p>\n