-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgutenberg_scraping.py
239 lines (157 loc) · 6.09 KB
/
gutenberg_scraping.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import streamlit as st
import pandas as pd
import requests
from bs4 import BeautifulSoup
from bs4.dammit import EncodingDetector
# Diese Modul wird verwendet um Projekt Gutenberg zu scrapen
# Verschiedene Funktionalität für die einzelnen Seiten (Autoren, Buch) wird zur Verfügung gestellt
# Die Start-URL für alle Anfragen
BASE_URL = "https://www.projekt-gutenberg.org"
@st.cache_resource
def scrape_autor(author): # Only Function that should be accessed from outside the modul
"""
Extrahiert alle Informationen über den Autor 'author' in Projekt Gutenberg.
Parameters:
author (str): Der Name des Autors
Returns:
dic (dict): Ein Wörterbuch mit allen relevanten Informationen zum Autor
"""
url = f"{BASE_URL}/autoren/namen/{author.lower()}.html"
print(f"Scrape Autor {author} [{url}]")
res = requests.get(url)
# Autor nicht gefunden
if res.status_code != 200:
print(f"Autor {author} wurde nicht gefunden.")
return None
# Scraping mit BeautifulSoup
try:
print(f"Autor {author} wurde gefunden.")
author_site = BeautifulSoup(res.content, 'lxml', from_encoding= EncodingDetector.find_declared_encoding(res.content, is_html=True))
# Decoding Fehler
except Exception:
print("Error while decoding page")
return None
# Wörterbuch mit den extrahierten Informationen
infos = {"data" : None,
"books" : _find_books(author_site),
"info" : _find_info(author_site),
"image_url" : _find_image(author_site)
}
df_all = pd.DataFrame()
# Text aller Bücher
for title, url in infos["books"]:
st.markdown(f"[{title}]({url})")
print(f"Scrape Buch '{title}' [{url}]")
# Das Buch scrapen
df_temp = _scrape_book(url)
# Die Dataframes kombinieren
df_all = pd.concat([df_all,df_temp], ignore_index=True)
df_all["Autor"] = author.upper()
infos["data"] = df_all
print(f"Gefundene Sätze: {df_all.shape}")
return infos
def _scrape_book(url):
"""
Führt das Scrapen des Buches aus der URL aus.
Parameters:
url (str): Die Addresse des Buches
Returns:
df (DataFrame): Ein DataFrame mit allen Sätzen
"""
res = requests.get(url)
book_site = BeautifulSoup(res.content, 'lxml', from_encoding=EncodingDetector.find_declared_encoding(res.content, is_html=True))
subchapters = book_site.find_all("li")
print(f"\tAnzahl Unterkapitel: {len(subchapters)}")
subchapters_links = []
for sub in subchapters:
link = sub.find("a", href=True)
subchapters_links.append(url + "/" + link["href"])
df = pd.DataFrame(columns=["Satz"])
progressbar = st.progress(0)
for index, temp_url in enumerate(subchapters_links):
# Zeige die Progressbar an
progressbar.progress((index+1)/len(subchapters_links))
res = requests.get(temp_url)
books = BeautifulSoup(res.content, 'lxml', from_encoding=EncodingDetector.find_declared_encoding(res.content, is_html=True))
data = _find_text(books)
for satz in data.split("."):
df.loc[len(df)] = satz
# Löschen der ProgressBar
progressbar.empty()
df["Satz"] = df["Satz"].map(_correction).dropna()
return df
def _find_image(author_site):
"""
Extrahiert den Link zum Autorenbild aus dem Quellcode für eine Autorenseite,
siehe z.B. den HTML-Code von: https://www.projekt-gutenberg.org/autoren/namen/kafka.html
Falls ein Bild gefunden wurde, wird die URL zurückgegeben.
Parameters:
books (str): Der HTML-Code der Bücher, die vom Autor geschrieben sind
Returns:
Die Addresse des Bildes oder None
"""
try:
return f"{BASE_URL}/autoren/{author_site.find('img', src=True, title=True)['src'][3:]}"
except:
return None
def _find_info(author_site):
"""
Extrahiert Informationen über den Autor aus dem Quellcode für eine Autorenseite,
siehe z.B. https://www.projekt-gutenberg.org/autoren/namen/kafka.html
Falls Informationen gefunden wurden, werden diese als Text zurückgegeben.
Parameters:
books (str): Der HTML-Code der Bücher, die vom Autor geschrieben sind
Returns:
Alle Paragraphe oder None
"""
try:
return author_site.find_all("p")[1].text
except:
return None
def _find_text(books):
"""
Liefert den Text eines Onlinebuches, was schon durch BeautifulSoup geparst ist.
Parameters:
books (dict): Der HTML-Code aller Bücher
Returns:
text (str): Der String mit allen Texten
"""
text = ""
# Findet alle HTML Paragraphe mit dem Tag <\p>
for paragraph in books.find_all("p"):
if paragraph.string:
text += paragraph.text
return text
def _find_books(books):
"""
Liefert alle Bücher, die der Autor geschrieben hat.
Parameters:
books (str): Der HTML-Code der Bücher, die vom Autor geschrieben sind
Returns:
book_url (list): Eine Liste von Tupels des Titels und der URL
"""
tag = books.find("div", {"class":"archived"})
if tag == None:
return []
book_url = []
# Die Bücher findet man unter der Liste mit dem Tag <li>
for l in tag.find_all("li"):
tag = l.find("a",href=True)
book_title = tag.string
url = f"{BASE_URL}/{tag['href'][6:]}"
url = url[:url.rfind("/")]
book_url.append((book_title, url))
return book_url
def _correction(string):
"""
Die Funktion filtert Sätze die kleiner als 4 Zeichen sind aus der Analyse.
Ergänzbar um weitere Kriterien.
Parameters:
string (str): Der Satz zum filtern
Returns:
Entweder den String oder None.
"""
if len(string)< 4:
return None
else:
return string