| 1 |
|
| 2 |
|
| 3 |
import urllib.request |
| 4 |
import json |
| 5 |
from bs4 import BeautifulSoup |
| 6 |
|
| 7 |
url = "https://en.wikiquote.org/wiki/Wikiquote:Quote_of_the_day/Complete_list" |
| 8 |
response = urllib.request.urlopen(url) |
| 9 |
text = response.read().decode("utf-8") |
| 10 |
|
| 11 |
soup = BeautifulSoup(text, 'html.parser') |
| 12 |
lists = soup.find_all("ul") |
| 13 |
items = list() |
| 14 |
json_feed = "{{\"title\": {title}, \"items\": [{items}]}}" |
| 15 |
|
| 16 |
for lst in lists: |
| 17 |
try: |
| 18 |
last_link = lst.find_all("a")[-1] |
| 19 |
quote_link = last_link.get("href") |
| 20 |
|
| 21 |
if quote_link.startswith("/"): |
| 22 |
quote_link = "https://en.wikiquote.org" + quote_link |
| 23 |
|
| 24 |
quote_author = last_link.get_text() |
| 25 |
|
| 26 |
if not quote_author: |
| 27 |
continue |
| 28 |
|
| 29 |
quote_text = "<span>" + lst.find("li").decode_contents() + "</span>" |
| 30 |
quote_heading = lst.find("li") |
| 31 |
quote_heading = (quote_heading.get_text()[:75] + '...') if len(quote_heading) > 75 else quote_heading.get_text() |
| 32 |
quote_heading = quote_heading.split(" ~")[0] |
| 33 |
|
| 34 |
items.append("{{\"title\": {title}, \"authors\": [{{\"name\": {author}}}], \"content_html\": {html}, \"url\": {url}, \"date_published\": {date}}}".format( |
| 35 |
title = json.dumps(quote_heading), |
| 36 |
html = json.dumps(quote_text), |
| 37 |
url = json.dumps(quote_link), |
| 38 |
author = json.dumps(quote_author), |
| 39 |
date = json.dumps("2020-12-31T08:00:00"))) |
| 40 |
except: |
| 41 |
continue |
| 42 |
|
| 43 |
json_feed = json_feed.format(title = json.dumps(soup.title.text), items = ", ".join(items)) |
| 44 |
print(json_feed) |