-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourse_paper.py
58 lines (43 loc) · 1.7 KB
/
course_paper.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
import requests
from bs4 import BeautifulSoup
import csv
import os
URL = 'https://citaty.info/book/quotes'
HEADERS = {'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0',
'accept': '*/*'}
FILE_CSV = 'quotations_csv.csv'
def get_html(url, params=None):
r = requests.get(url, headers=HEADERS, params=params)
return r
def get_content(html):
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('div', class_='node__content')
quotations = []
for item in items:
topic_tags = item.find('div', class_='node__topics')
if topic_tags:
topic_tags = topic_tags.get_text(', ')
else:
topic_tags = 'No tags'
quotations.append({
'autor': item.find('a', title='Автор цитаты').get_text(),
'book': item.find('a', title='Цитата из книги').get_text(),
'text_quotation': item.find('div', class_='field-item even last').get_text().replace('\xa0', ' ').replace('\n', ' '),
'topic_tags': topic_tags
})
print(*quotations, sep='\n')
save_file_csv(quotations, FILE_CSV)
os.startfile(FILE_CSV)
def save_file_csv (items, path):
with open(path, 'w', newline='') as file:
writer=csv.writer(file, delimiter=';')
writer.writerow(['Autor', 'Book', 'Quotation', 'Tags'])
for item in items:
writer.writerow([item['autor'], item['book'], item['text_quotation'], item['topic_tags']])
def parse():
html = get_html(URL)
if html.status_code == 200:
get_content(html.text)
else:
print('Error!')
parse()