Created
April 22, 2023 10:14
-
-
Save tobixen/1b3cc83749905aa4ff777e171c172aa1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import requests | |
def count_chars(text): | |
"""Count the frequency of each character in the given text.""" | |
freq = {} | |
for char in text: | |
freq[char] = freq.get(char, 0) + 1 | |
return freq | |
r = requests.get("https://no.wikipedia.org/wiki/Spesial:Tilfeldig", allow_redirects=False) | |
random_page_url = r.headers['location'] | |
random_page_title = random_page_url.split('/')[-1] | |
text_json = requests.get(f"https://no.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles={random_page_title}&rvslots=main").json() | |
pages_data = text_json['query']['pages'].values() | |
for page_data in pages_data: | |
for revision_data in page_data['revisions']: | |
page_text = revision_data['slots']['main']['*'] | |
freq = count_chars(page_text) | |
for char, count in freq.items(): | |
print(f'{count:3d} {char}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment