-
Notifications
You must be signed in to change notification settings - Fork 0
/
poem
executable file
·31 lines (22 loc) · 887 Bytes
/
poem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
poem_of_the_day_url = 'https://www.poetryfoundation.org/poems/poem-of-the-day'
#harlem = 'https://www.poetryfoundation.org/poems/46548/harlem'
html = requests.get(poem_of_the_day_url).text
soup = BeautifulSoup(html, "html.parser")
#poem is too long, go to full page
readmore = soup.find('a',{'class':'c-txt c-txt_minimalCta'})
if readmore.getText().strip() == "Read More":
html = requests.get(readmore.get('href')).text
soup = BeautifulSoup(html, "html.parser")
#find title
print(soup.find('div',{'class':'c-feature-hd'}).getText().strip())
#find author
print(soup.find('div',{'class':'c-feature-sub c-feature-sub_vast'}).getText().strip())
print()
#code for poem
poem_raw = soup.find('div', {'class':'o-poem'}).findAll('div')
for line in poem_raw:
print(line.getText())