Skip to content

Instantly share code, notes, and snippets.

@harendra21
Created February 2, 2022 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harendra21/baaecd9d6a534807a4052eaa1fbc7b1d to your computer and use it in GitHub Desktop.
Save harendra21/baaecd9d6a534807a4052eaa1fbc7b1d to your computer and use it in GitHub Desktop.
from bs4 import BeautifulSoup
import requests
# Trying to open a random wikipedia article
# Special:Random opens random articles
res = requests.get("https://en.wikipedia.org/wiki/Special:Random")
res.raise_for_status()
# pip install htmlparser
wiki = BeautifulSoup(res.text, "html.parser")
r = open("random_wiki.txt", "w+", encoding='utf-8')
# Adding the heading to the text file
heading = wiki.find("h1").text
r.write(heading + "\n")
for i in wiki.select("p"):
# Optional Printing of text
# print(i.getText())
r.write(i.getText())
r.close()
print("File Saved as random_wiki.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment