-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_scraper.py
47 lines (31 loc) · 1.08 KB
/
module_scraper.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from bs4 import BeautifulSoup
from urllib.request import urlopen
import json
import random
def clean(data, allowed_symbols):
# Replace % symbol with the word "percent"
data = data.replace("%", " percent")
data = data.upper()
uniques = set(data)
# remove symbols
for u in uniques:
if u not in allowed_symbols:
data = data.replace(u, "")
return data
def get_nature_briefings():
url = "https://www.nature.com/nature/articles?type=nature-briefing"
page = urlopen(url)
html = page.read().decode("utf-8")
soup = BeautifulSoup(html, "html.parser")
# Get short summaries
results = soup.find_all("div", {"data-test": "article-description"})
results = [r.text.strip() for r in results]
# Get their dates
dates = soup.find_all("time", {"itemprop": "datePublished"})
dates = [d.text.strip() for d in dates]
return results, dates
def get_shakespeare(sonnet_nr):
with open("data/shakespeare.json", "r") as f:
contents = f.read()
sonnets = json.loads(contents)
return sonnets[sonnet_nr - 1]