Дан некоторый URL:
url = 'test.com/dir1/dir2/dir3/page.html'
Получите из него имя страницы:
'page.html'
import requests
from bs4 import BeautifulSoup
def get_page_title(url):
try:
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
title_tag = soup.find('title')
if title_tag:
return title_tag.string.strip()
else:
return "Заголовок не найден"
except requests.exceptions.RequestException as e:
return f"Ошибка при загрузке URL: {e}"
except Exception as e:
return f"Произошла ошибка: {e}"
test_url = "http://test.com/dir1/dir2/dir3/page.html"
page_title = get_page_title(test_url)
print(f"Заголовок страницы для {test_url}: {page_title}")
Заголовок страницы для : Ошибка при загрузке URL: Invalid URL '': No scheme supplied. Perhaps you meant https://?