-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathczytanieisluchanie.py
More file actions
200 lines (124 loc) · 6.79 KB
/
Copy pathczytanieisluchanie.py
File metadata and controls
200 lines (124 loc) · 6.79 KB
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
#%% import
from __future__ import unicode_literals
import requests
from bs4 import BeautifulSoup
import pandas as pd
import regex as re
import time
from tqdm import tqdm #licznik
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
import json
#%% def
def get_article_links(sitemap_link):
html_text = requests.get(sitemap_link).text
soup = BeautifulSoup(html_text, 'lxml')
links = [e.text for e in soup.find_all('loc')]
return links
def dictionary_of_article(article_link):
# article_link = 'https://czytanieisluchanie.blogspot.com/2025/05/obszerne-obserwowanie.html'
#article_link = 'https://czytanieisluchanie.blogspot.com/2025/04/kwestia-gramatyki.html'
# article_link = 'https://czytanieisluchanie.blogspot.com/2024/03/w-srodku-byocicho.html'
# article_link = 'https://czytanieisluchanie.blogspot.com/2022/11/przymknijcie-oczy-otworzcieserca.html'
# article_link = 'https://czytanieisluchanie.blogspot.com/2019/08/woski-sen.html'
html_text = requests.get(article_link).text
while 'Error 503' in html_text:
time.sleep(2)
html_text = requests.get(article_link).text
soup = BeautifulSoup(html_text, 'lxml')
try:
author = soup.find('span', class_='post-author-label').text.strip()
except:
author = None
if author == 'Posted by BooksJarkaHoldena':
author = 'Jarek Holden'
try:
title_of_article = soup.find('h3', class_='post-title entry-title').text.strip()
except:
title_of_article = None
try:
date_of_publication = soup.find('a', class_='timestamp-link').time['datetime'][:10]
except:
date_of_publication = None
try:
tags = " | ".join([x.text for x in soup.find('span', class_='byline post-labels').find_all('a', {'rel':'tag'})])
except:
tags = None
article = soup.find('div', class_='post-body-container')
try:
text_of_article = article.text.replace("\n", " ").replace(" ", " ").strip().replace('\xa0', ' ')
except:
text_of_article = None
try:
book_description_end = re.search(r"\d{4}\.?", text_of_article).end()
book_description = text_of_article[:book_description_end]
except:
book_description = None
author_of_book = None
if book_description:
patterns = [
# 1. Tytuł w cudzysłowie, potem autor i liczba stron
r'["”]{1,2}.*?["”]{1,2}\.?\s*([A-ZŻŹĆĄŚĘŁÓŃ][^,\.]+?),\s*\d+\s*stron',
# 2. Autor po ostatniej kropce w tytule (wielokrotne zdania w tytule)
r'\.\s*([A-ZŻŹĆĄŚĘŁÓŃ][^,]+?),\s*\d+\s*stron',
# 3. Po cudzysłowie i kropce (np. „Tytuł”. Autor.)
r'[”"]\s*\.?\s*([A-ZŻŹĆĄŚĘŁÓŃ][^.,;\(\)]+?)(?=\.\s*(Przekład|tłum|redakcja|Wydawnictwo|$))',
# 4. Po tytule zakończonym kropką (np. Tytuł. Autor.)
r'^[^\.]+?\.\s*([A-ZŻŹĆĄŚĘŁÓŃ][^.,;\(\)]+?)(?=\.\s*(Przekład|tłum|redakcja|Wydawnictwo|$))',
# 5. Po myślniku (np. Tytuł - Autor, ...)
r'-\s*([A-ZŻŹĆĄŚĘŁÓŃ][^,;\.\(\)]+)',
# 6. Po tytule z nawiasem (np. Tytuł (język), Autor.)
r'\)\s*,?\s*([A-ZŻŹĆĄŚĘŁÓŃ][^.,;\(\)]+?)(?=\.\s*(Przekład|tłum|redakcja|Wydawnictwo|$))',
# 7. Po kropce, przed „tłumacz” lub „redakcją” (np. Autor. tłumacz...)
r'([A-ZŻŹĆĄŚĘŁÓŃ][^.,;\(\)]+?)\.\s*(tłum|redakcja)',
# 8. Autor przed przecinkiem i liczbą stron (poprawiony, aby uwzględnić polskie znaki i różne znaki)
r'([A-ZŻŹĆĄŚĘŁÓŃ][\w\s\-\.’’]+?),\s*\d+\s*stron[ay]?',
# NOWE wzorce - dopasowanie autora po tytule zakończonym kropką lub przecinkiem
# 1. Tytuł. Autor. Wydawnictwo, Miasto Rok.
r'^[^.,\n]+[.,]\s*([A-ZŻŹĆĄŚĘŁÓŃ][\w\s\.\-’\'`]+)\.\s*[A-ZĄĆĘŁŃÓŚŹŻ].*?\d{4}',
# 2. Tytuł, Autor. Wydawnictwo, Miasto Rok.
r'^[^.,\n]+,\s*([A-ZŻŹĆĄŚĘŁÓŃ][\w\s\.\-’\'`]+)\.\s*[A-ZĄĆĘŁŃÓŚŹŻ].*?\d{4}',
# 3. „Tytuł”, Autor. Wydawnictwo ...
r'[”"]\s*,\s*([A-ZŻŹĆĄŚĘŁÓŃ][\w\s\.\-’\'`]+)\.\s*[A-ZĄĆĘŁŃÓŚŹŻ].*?\d{4}',
]
for pattern in patterns:
try:
match = re.search(pattern, book_description)
if match:
author_of_book = match.group(1).strip()
break
except:
continue
try:
external_links = ' | '.join([x for x in [x['href'] for x in article.find_all('a')] if not re.findall(r'blogger|blogspot|czytanieisluchanie', x)])
except (AttributeError, KeyError, IndexError):
external_links = None
try:
photos_links = ' | '.join([x['src'] for x in article.find_all('img')])
except (AttributeError, KeyError, IndexError):
photos_links = None
dictionary_of_article = {'Link': article_link,
'Data publikacji': date_of_publication,
'Autor': author,
'Tytuł artykułu': title_of_article,
'Opis książki': book_description,
'Autor książki': author_of_book,
'Tekst artykułu': text_of_article,
'Tagi': tags,
'Linki zewnętrzne': external_links,
'Zdjęcia/Grafika': True if [x['src'] for x in article.find_all('img')] else False,
'Filmy': True if [x['src'] for x in article.find_all('iframe')] else False,
'Linki do zdjęć': photos_links}
all_results.append(dictionary_of_article)
#%% main
article_links = get_article_links('https://czytanieisluchanie.blogspot.com/sitemap.xml')
all_results = []
with ThreadPoolExecutor() as excecutor:
list(tqdm(excecutor.map(dictionary_of_article, article_links),total=len(article_links)))
df = pd.DataFrame(all_results).drop_duplicates()
df["Data publikacji"] = pd.to_datetime(df["Data publikacji"]).dt.date
df = df.sort_values('Data publikacji', ascending=True)
with open(f'data/czytanieisluchanie_{datetime.today().date()}.json', 'w', encoding='utf-8') as f:
json.dump(all_results, f, ensure_ascii=False)
with pd.ExcelWriter(f"data/czytanieisluchanie_{datetime.today().date()}.xlsx", engine='xlsxwriter') as writer:
df.to_excel(writer, 'Posts', index=False)