Spaces:
Running
Running
Create src/url_fetcher.py
Browse files- src/url_fetcher.py +14 -12
src/url_fetcher.py
CHANGED
@@ -1,17 +1,19 @@
|
|
1 |
import requests
|
2 |
-
import
|
3 |
|
4 |
def fetch_url_title(url):
|
5 |
try:
|
6 |
response = requests.get(url)
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
1 |
import requests
|
2 |
+
import logging
|
3 |
|
4 |
def fetch_url_title(url):
|
5 |
try:
|
6 |
response = requests.get(url)
|
7 |
+
response.raise_for_status()
|
8 |
+
|
9 |
+
title = "No title found"
|
10 |
+
if response.content:
|
11 |
+
# Simple title extraction (you could use BeautifulSoup for more complex parsing)
|
12 |
+
title_start = response.content.find(b"<title>")
|
13 |
+
title_end = response.content.find(b"</title>")
|
14 |
+
if title_start != -1 and title_end != -1:
|
15 |
+
title = response.content[title_start + 7:title_end].decode("utf-8")
|
16 |
+
|
17 |
+
logging.info(f"Fetched title: {title} from URL: {url}")
|
18 |
+
except requests.exceptions.RequestException as e:
|
19 |
+
logging.error(f"Error fetching URL {url}: {e}")
|