-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdwutygodnik.py
More file actions
173 lines (140 loc) · 6.05 KB
/
Copy pathdwutygodnik.py
File metadata and controls
173 lines (140 loc) · 6.05 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
#%%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
from functions import date_change_format_long
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
#%%def
def process_sitemap(sitemap):
html_text_sitemap = requests.get(sitemap).text
soup = BeautifulSoup(html_text_sitemap, 'lxml')
links = [e.text for e in soup.find_all('loc')]
return links
def dictionary_of_article(article_link):
response = requests.get(article_link)
if not response.ok:
return
html_text = response.text
soup = BeautifulSoup(html_text, 'lxml')
categories = ['cat--literatura', 'cat--film', 'cat--teatr', 'cat--felietony']
for cat in categories:
if soup.find('a', class_=cat):
break
else: return
# title + section
title_of_article, section = [e.strip() for e in soup.title.text.split('|')[:2]]
# description
description = soup.find('meta', {'name': 'description'})['content']
if section == 'felieton':
# author
if author := soup.find_all('h2', class_='article-aside-feuilleton__author'):
author = list(set([e.text.strip() for e in author]))
author = ' | '. join(author)
else:
author = None
# date
date_of_publication = None
# issue
issue = None
# tags
if tags := soup.find('div', class_='article-property__value--tags'):
tags = [e.text.strip() for e in tags.find_all('a')]
tags = ' | '.join(tags)
else:
tags = None
# content
if art_content := soup.find('div', class_='article-content'):
text_of_article = art_content
article = text_of_article.text.strip().replace('\n', ' ')
else:
text_of_article, article = None, None
try:
external_links = ' | '.join([x for x in [x['href'] for x in text_of_article.find_all('a') if x['href']]])
except (AttributeError, KeyError, IndexError):
external_links = None
try:
photos_links = ' | '.join([x['src'] for x in text_of_article.find_all('img') if x['src']])
except (AttributeError, KeyError, IndexError):
photos_links = None
else:
# author
if author := soup.find_all('a', class_='article-property__author-link'):
author = list(set([e.text.strip() for e in author]))
author = ' | '. join(author)
else:
author = None
# date
if date_of_publication := soup.find('div', class_='article-property__value--date'):
date_of_publication = date_of_publication.text
else:
date_of_publication = None
# issue
if issue := soup.find('div', class_='article-property__value--issue'):
issue = issue.text
else:
issue = None
# tags
if tags := soup.find('div', class_='article-property__value--tags'):
tags = [e.text.strip() for e in tags.find_all('a')]
tags = ' | '.join(tags)
else:
tags = None
# content
if art_content := soup.find('div', class_='article-content'):
text_of_article = art_content
article = text_of_article.text.strip().replace('\n', ' ')
else:
text_of_article, article = None, None
try:
external_links = ' | '.join([x for x in [x['href'] for x in text_of_article.find_all('a') if x['href']]])
except (AttributeError, KeyError, IndexError):
external_links = None
try:
photos_links = ' | '.join([x['src'] for x in text_of_article.find_all('img') if x['src']])
except (AttributeError, KeyError, IndexError):
photos_links = None
dictionary_of_article = {'Link': article_link,
'Data publikacji': date_of_publication,
'Sekcja': section,
'Autor': author,
'Tytuł artykułu': title_of_article,
'Opis': description,
'Numer': issue,
'Tagi': tags,
'Tekst artykułu': article,
'Linki zewnętrzne': external_links,
'Linki do zdjęć': photos_links,
}
all_results.append(dictionary_of_article)
#%%
articles_links = []
sitemap_url = 'https://www.dwutygodnik.com/sitemap/'
sitemap_links = process_sitemap(sitemap_url)
articles_links = [link for link in sitemap_links if '/artykul/' in link]
all_results = []
with ThreadPoolExecutor() as excecutor:
list(tqdm(excecutor.map(dictionary_of_article, articles_links),total=len(articles_links)))
with open(f'data/dwutygodnik_{datetime.today().date()}.json', 'w', encoding='utf-8') as f:
json.dump(all_results, f, ensure_ascii=False)
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=False)
with pd.ExcelWriter(f"data/dwutygodnik_{datetime.today().date()}.xlsx", engine='xlsxwriter', options={'strings_to_urls': False}) as writer:
df.to_excel(writer, 'Posts', index=False, encoding='utf-8')
writer.save()
#%%Uploading files on Google Drive
gauth = GoogleAuth()
drive = GoogleDrive(gauth)
upload_file_list = [f"data/dwutygodnik_{datetime.today().date()}.xlsx", f'data/dwutygodnik_{datetime.today().date()}.json']
for upload_file in upload_file_list:
gfile = drive.CreateFile({'parents': [{'id': '19t1szTXTCczteiKfF2ukYsuiWpDqyo8f'}]})
gfile.SetContentFile(upload_file)
gfile.Upload()