The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python wikipedia module: How to get wikipedia page content and other useful data fields from wikipedia module - page images, links, categories and ref
By Guest on 18th December 2022 04:21:37 PM | Syntax: PYTHON | Views: 133



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. To get the content of a Wikipedia page using the Wikipedia module in Python, you can use the page.content attribute. This attribute returns the full text of the page as a string. Here's an example of how you can get the content of a page and print it:
  2.  
  3.  
  4. import wikipedia
  5.  
  6. # Search for pages that match the search term
  7. results = wikipedia.search("search term")
  8.  
  9. # Get the best page from the search results
  10. page = wikipedia.page(results[0])
  11.  
  12. # Print the page's content
  13. print(page.content)
  14.  
  15.  
  16.  
  17. You can also access other useful data fields for a Wikipedia page using the page object. Here are some examples of data fields that you can access:
  18.  
  19. page.title: the title of the page
  20. page.url: the URL of the page
  21. page.summary: a summary of the page's content
  22. page.images: a list of URLs for images on the page
  23. page.links: a list of URLs for links on the page
  24. page.references: a list of URLs for references on the page
  25. page.categories: a list of categories for the page
  26.  
  27. Here's an example of how you can access and print some of these data fields:
  28.  
  29.  
  30. import wikipedia
  31.  
  32. # Search for pages that match the search term
  33. results = wikipedia.search("search term")
  34.  
  35. # Get the best page from the search results
  36. page = wikipedia.page(results[0])
  37.  
  38. # Print the page's title, URL, summary, and list of images
  39. print(page.title)
  40. print(page.url)
  41. print(page.summary)
  42. print(page.images)
  43.  
  44.  
  45. You can access any of the data fields listed above using the same syntax. Just replace field_name with the name of the field you want to access.
  46.  
  47.  
  48.  
  49. Watch: How to Make a Wikipedia Client in python - Wikipedia API tutorial:
















Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.

Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.

See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license
  • Recent Pastes