Created
February 2, 2022 06:36
-
-
Save harendra21/baaecd9d6a534807a4052eaa1fbc7b1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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