Wikipedia Article Fetcher
import wikipedia
def fetch_article(topic):
try:
print(f"\nš Searching for '{topic}'...")
summary = wikipedia.summary(topic, sentences=3)
page = wikipedia.page(topic)
print("\nš Summary:")
print(summary)
print("\nš URL:", page.url)
more = input("\nDo you want to read the full article? (y/n): ").strip().lower()
if more == 'y':
print("\nš Full Content:\n")
print(page.content[:2000] + '...') # Show first 2000 characters
except wikipedia.exceptions.DisambiguationError as e:
print("ā Disambiguation error. Please be more specific.")
print("Options include:", e.options[:5]) # Show top 5 suggestions
except wikipedia.exceptions.PageError:
print("ā Page not found. Please check the topic spelling.")
except Exception as e:
print(f"ā ļø Error: {e}")
def main():
print("--- Wikipedia Article Fetcher ---")
topic = input("Enter a topic to search: ").strip()
fetch_article(topic)
if __name__ == "__main__":
main()
Code output
--- Wikipedia Article Fetcher ---
Enter a topic to search: Python programming
š Summary:
Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability...
š URL: https://en.wikipedia.org/wiki/Python_(programming_language)
Do you want to read the full article? (y/n): n