Skip to content

Instantly share code, notes, and snippets.

@jokull
Forked from rexbannon/gist:115f74c845f27b9fc14522cb13dc16c5
Created October 29, 2020 21:57
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 jokull/d05b762372706acc68e9ab7f5332e5f4 to your computer and use it in GitHub Desktop.
Save jokull/d05b762372706acc68e9ab7f5332e5f4 to your computer and use it in GitHub Desktop.
Ljodafundur
import requests
import re
from bs4 import BeautifulSoup
import random
URL = "http://www.ljod.is/index.php/ljod/poem_collection/author"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
authors = []
for link in [a["href"] for a in soup.find_all("a")]:
if re.search("view_poet", link):
authors.append(link)
author_page = requests.get(random.choice(authors))
soup = BeautifulSoup(author_page.content, "html.parser")
poems = []
for link in [a["href"] for a in soup.find_all("a")]:
if re.search("view_poem", link):
poems.append(link)
ljoda_page = requests.get(random.choice(poems))
soup = BeautifulSoup(ljoda_page.content, "html.parser")
ljodid = soup.find("span", class_="poem")
print(ljodid.text.strip())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment