-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-scrapping.py
145 lines (122 loc) · 5.04 KB
/
web-scrapping.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
# код веб-скрапинга, с помощью которого были получены все таблицы в csv
# файлах в этом проекте
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://briefly.ru'
responce = requests.get(url + '/authors/')
soup = BeautifulSoup(responce.content, 'html.parser')
authors_html_block = soup.find('div', 'alphabetic-index')
authors = authors_html_block.find_all('a')
authors_codes2names = dict()
author_codes = []
for author in authors:
if 'surnames' not in author['href']:
author_codes.append(author['href'])
authors_codes2names[author['href']] = author.text
continue
surnames_responce = requests.get(url + author['href'])
surnames_soup = BeautifulSoup(surnames_responce.content, 'html.parser')
for author_html_block in surnames_soup.find_all('div', 'author'):
author_code = author_html_block.find('a')['href']
author_codes.append(author_code)
authors_codes2names[author_code] = author.text
# блок cо скрапингом сайта может работать до 5 - 10 минут
main_tags = []
book_titles = []
book_authors = []
book_years = []
book_tags = []
book_summary_urls = []
book_original_urls = []
book_texts = []
for author_code in author_codes:
print(author_code)
author_responce = requests.get(url + author_code)
author_soup = BeautifulSoup(author_responce.content, 'html.parser')
author_name = author_soup.find(
'span', 'author_name normal' if author_soup.find(
'span', 'author_name normal') else 'author_name long').text.replace(
'\xa0', ' ')
tags = [
tag.text.replace(
'\xa0',
' ') for tag in author_soup.find(
'ol',
'breadcrumbs-compact').find_all('span')]
main_tags.append(tags[0])
# если у автора нет книг
if author_soup.find('section', "author_works").find('div', 'noworks'):
continue
# определяем есть ли на странице блок "все пересказы по алфавиту"
if author_soup.find('section', "works_index"):
has_works_index_section = True
book_html_blocks = author_soup.find(
'section', "works_index").find_all('li')
else:
has_works_index_section = False
book_html_blocks = author_soup.find(
'section', 'author_works').find_all(
'div', 'w-featured')
# перебираем все книги автора
for book_html_block in book_html_blocks:
if "requested" in book_html_block['class'] or "pending" in book_html_block['class']:
continue
book_code = book_html_block.find('a')['href']
# переходим на суп страницы с книгой
book_responce = requests.get(url + book_code)
book_soup = BeautifulSoup(book_responce.content, 'html.parser')
# парсим краткое содержание
book_paragraphs = []
if book_soup.find('p', 'microsummary'):
book_paragraphs.append(
book_soup.find(
'p', 'microsummary').get_text())
book_paragraphs.extend([paragraph.text for paragraph in book_soup.find(
'div', id='text').find_all(['h2', 'h3', 'p', 'blockquote', 'li'])])
text = '\n'.join(book_paragraphs).replace(
'\xad', '').replace('\xa0', ' ')
book_texts.append(text)
# добавляем всю остальную информации по книге
book_tag = book_soup.find(
'div', 'breadcrumb__content').text.replace(
'\xa0', ' ')
main_tags.append(book_tag)
book_tags.append(', '.join(tags + [book_tag]))
book_authors.append(author_name)
book_years.append(
book_soup.find(
'span',
'date').text if book_soup.find(
'span',
'date') else None)
book_summary_urls.append(url + book_code)
book_original_urls.append(
book_soup.find(
'div',
'readingtime').find('a')['href'] if book_soup.find(
'div',
'readingtime').find('a') else None)
book_titles.append(
book_html_block.find('a').text.replace(
'\xa0', ' ') if has_works_index_section else book_html_block.find(
'div', 'w-title').text.replace(
'\xa0', ' '))
books_table = pd.DataFrame(
{
"title": book_titles,
"author": book_authors,
"year": book_years,
"tags": book_tags,
"summary": book_texts,
"summary url": book_summary_urls,
"original url": book_original_urls
}
)
tags_table = pd.DataFrame({"tag": sorted(list(set(main_tags)))})
tags_table = tags_table[tags_table.apply(
lambda row: 'Проч' not in row['tag'],
axis=1
)]
tags_table.to_csv('tags_table.csv', encoding='utf-8', index=False)
books_table.to_csv('books_table.csv', encoding='utf-8', index=False)