Skip to content

Instantly share code, notes, and snippets.

@tobixen
Created April 22, 2023 10:14
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 tobixen/1b3cc83749905aa4ff777e171c172aa1 to your computer and use it in GitHub Desktop.
Save tobixen/1b3cc83749905aa4ff777e171c172aa1 to your computer and use it in GitHub Desktop.
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