Skip to content

Commit 02f5b91

Browse files
committed
step 6 done.
1 parent d55693a commit 02f5b91

File tree

4 files changed

+89
-30
lines changed

4 files changed

+89
-30
lines changed

page_analyzer/app.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import psycopg2
55
from dotenv import load_dotenv
66
import validators
7+
from bs4 import BeautifulSoup
8+
79

810

911
load_dotenv()
@@ -52,7 +54,8 @@ def urls():
5254
# Объединяем запросы для получения всех URL и последней проверки
5355
cur.execute('''
5456
SELECT u.id, u.name, u.created_at,
55-
uc.status_code, uc.created_at AS last_check
57+
uc.status_code, uc.created_at AS last_check,
58+
uc.h1, uc.title, uc.description
5659
FROM urls u
5760
LEFT JOIN url_checks uc ON u.id = uc.url_id
5861
AND uc.created_at = (
@@ -86,12 +89,11 @@ def url_detail(url_id):
8689
return render_template('result.html', url=url, checks=checks)
8790

8891

89-
@app.route('/urls/<int:url_id>/checks', methods=['POST'])
92+
@app.route('/check_url/<int:url_id>', methods=['POST'])
9093
def add_check(url_id):
9194
conn = get_db_connection()
9295
cur = conn.cursor()
9396

94-
# Получаем URL из базы данных
9597
cur.execute('SELECT name FROM urls WHERE id = %s', (url_id,))
9698
url = cur.fetchone()
9799

@@ -104,16 +106,33 @@ def add_check(url_id):
104106
try:
105107
response = requests.get(url_name)
106108
response.raise_for_status() # Проверка на ошибки HTTP
107-
status_code = response.status_code
108109

109-
# Записываем код статуса в базу данных
110-
cur.execute('INSERT INTO url_checks (url_id, status_code) VALUES (%s, %s)', (url_id, status_code))
111-
conn.commit()
110+
# Парсинг HTML с помощью BeautifulSoup
111+
soup = BeautifulSoup(response.text, 'html.parser')
112+
113+
# Извлечение SEO-данных
114+
h1_tag = soup.find('h1')
115+
title_tag = soup.find('title')
116+
description_tag = soup.find('meta', attrs={'name': 'description'})
117+
118+
h1_content = h1_tag.text if h1_tag else None
119+
title_content = title_tag.text if title_tag else None
120+
description_content = description_tag['content'] if description_tag else None
121+
112122

113-
flash(f'Проверка успешна! Код ответа: {status_code}')
123+
# Записываем код статуса и SEO-данные в базу данных
124+
try:
125+
cur.execute('''
126+
INSERT INTO url_checks (url_id, status_code, h1, title, description)
127+
VALUES (%s, %s, %s, %s, %s)
128+
''', (url_id, response.status_code, h1_content, title_content, description_content))
129+
conn.commit()
130+
except Exception as e:
131+
print(f'Ошибка при вставке данных: {e}')
132+
133+
flash(f'Проверка успешна! Код ответа: {response.status_code}')
114134
except requests.exceptions.RequestException as e:
115135
flash('Произошла ошибка при проверке.')
116-
# Логируем ошибку, если нужно
117136
print(f'Ошибка: {e}')
118137
finally:
119138
cur.close()
@@ -122,7 +141,6 @@ def add_check(url_id):
122141
return redirect(url_for('urls'))
123142

124143

125-
126144
if __name__ == '__main__':
127145
app.run(debug=True)
128146

page_analyzer/templates/urls.html

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,35 @@ <h1>Список URL</h1>
1515
{% endwith %}
1616
<div class="container mt-5">
1717
<h1 class="mb-4">Сайты</h1>
18-
<table data-test="urls" class="table table-bordered table-hover">
19-
<thead class="table-light">
18+
<table class="table">
19+
<thead>
20+
<tr>
21+
<th>ID</th>
22+
<th>URL</th>
23+
<th>Дата создания</th>
24+
<th>Код последней проверки</th>
25+
<th>Дата последней проверки</th>
26+
<th>H1</th>
27+
<th>Title</th>
28+
<th>Description</th>
29+
</tr>
30+
</thead>
31+
<tbody>
32+
{% for url in urls %}
2033
<tr>
21-
<th>ID</th>
22-
<th>URL</th>
23-
<th>Дата создания</th>
24-
<th>Код статуса последней проверки</th>
25-
<th>Дата последней проверки</th>
34+
<td>{{ url[0] }}</td>
35+
<td><a href="{{ url_for('url_detail', url_id=url[0]) }}">{{ url[1] }}</a></td>
36+
<td>{{ url[2] }}</td>
37+
<td>{{ url[3] if url[3] is not none else 'Нет проверок' }}</td>
38+
<td>{{ url[4] if url[4] is not none else 'Нет проверок' }}</td>
39+
<td>{{ url[5] if url[5] is not none else 'Нет данных' }}</td>
40+
<td>{{ url[6] if url[6] is not none else 'Нет данных' }}</td>
41+
<td>{{ url[7] if url[7] is not none else 'Нет данных' }}</td>
2642
</tr>
27-
</thead>
28-
<tbody>
29-
{% for url in urls %}
30-
<tr>
31-
<td>{{ url[0] }}</td>
32-
<td><a href="{{ url_for('url_detail', url_id=url[0]) }}">{{ url[1] }}</a></td>
33-
<td>{{ url[2] }}</td>
34-
<td>{{ url[3] if url[3] is not none else 'Нет проверок' }}</td>
35-
<td>{{ url[4] if url[4] is not none else 'Нет проверок' }}</td>
36-
</tr>
37-
{% endfor %}
38-
</tbody>
39-
</table>
43+
{% endfor %}
44+
</tbody>
45+
</table>
46+
4047
<a href="{{ url_for('index') }}" class="btn btn-secondary">Назад</a>
4148
</div>
4249
{% endblock %}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description = "Add your description here"
55
readme = "README.md"
66
requires-python = ">=3.10"
77
dependencies = [
8+
"beautifulsoup4>=4.13.4",
89
"flask>=3.1.0",
910
"gunicorn>=23.0.0",
1011
"psycopg2-binary>=2.9.10",

uv.lock

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)