-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbialafabryka.py
More file actions
132 lines (85 loc) · 4.52 KB
/
Copy pathbialafabryka.py
File metadata and controls
132 lines (85 loc) · 4.52 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
#%% import
import requests
from bs4 import BeautifulSoup
import pandas as pd
import 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 biala_fabryka_web_scraping_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 get_article_pages(link):
html_text_sitemap = requests.get(link).text
soup = BeautifulSoup(html_text_sitemap, 'lxml')
sitemap_links = [e.text for e in soup.find_all('loc')]
articles_links.extend(sitemap_links)
def dictionary_of_article(article_link):
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')
date_of_publication = soup.find('h2', class_='date-header').text
new_date = date_change_format_long(date_of_publication)
text_of_article = soup.find('div', class_='post-body entry-content')
article = text_of_article.text.strip().replace('\n', ' ')
title_of_article = soup.find('h3', class_='post-title entry-title').text.strip()
tags_span = soup.find_all('span', class_='post-labels')
tags = ' | '.join([tag.text.replace(',','').replace('Etykiety:', '') for tag in tags_span][0].strip().split('\n'))
try:
work_description = [x.text for x in text_of_article.find_all('h2')][0].strip().replace('\n', ' ')
except (AttributeError, KeyError, IndexError, TypeError):
work_description = None
try:
external_links = ' | '.join([x for x in [x['href'] for x in text_of_article.find_all('a')] if not re.findall(r'blogger|blogspot|bialafabryka', x)])
except (AttributeError, KeyError, IndexError):
external_links = None
try:
photos_links = ' | '.join([x['src'] for x in text_of_article.find_all('img')])
except (AttributeError, KeyError, IndexError):
photos_links = None
dictionary_of_article = {'Link': article_link,
'Data publikacji': new_date,
'Autor': 'Piotr Gajda' if re.findall(r'(Autor\:\sPiotr Gajda)|(\(pg\))', article) else "Krzysztof Kleszcz",
'Tytuł artykułu': title_of_article,
'Tekst artykułu': article,
'Opis książki/płyty': work_description,
'Tagi': tags,
'Linki zewnętrzne': external_links,
'Zdjęcia/Grafika': True if [x['src'] for x in text_of_article.find_all('img')] else False,
'Filmy': True if [x['src'] for x in text_of_article.find_all('iframe')] else False,
'Linki do zdjęć': photos_links}
all_results.append(dictionary_of_article)
#%% main
sitemap_links = biala_fabryka_web_scraping_sitemap('https://bialafabryka.blogspot.com/sitemap.xml')
articles_links = []
with ThreadPoolExecutor() as excecutor:
list(tqdm(excecutor.map(get_article_pages, sitemap_links),total=len(sitemap_links)))
all_results = []
with ThreadPoolExecutor() as excecutor:
list(tqdm(excecutor.map(dictionary_of_article, articles_links),total=len(articles_links)))
with open(f'biała_fabryka_{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"bialafabryka_{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"bialafabryka_{datetime.today().date()}.xlsx", f'biała_fabryka_{datetime.today().date()}.json']
for upload_file in upload_file_list:
gfile = drive.CreateFile({'parents': [{'id': '19t1szTXTCczteiKfF2ukYsuiWpDqyo8f'}]})
gfile.SetContentFile(upload_file)
gfile.Upload()