Canstralian commited on
Commit
a67066b
·
verified ·
1 Parent(s): 62d3546

Create src/url_fetcher.py

Browse files
Files changed (1) hide show
  1. src/url_fetcher.py +14 -12
src/url_fetcher.py CHANGED
@@ -1,17 +1,19 @@
1
  import requests
2
- import re
3
 
4
  def fetch_url_title(url):
5
  try:
6
  response = requests.get(url)
7
- if response.status_code == 200:
8
- match = re.search('<title>(.*?)</title>', response.text)
9
- if match:
10
- return match.group(1)
11
- else:
12
- return None
13
- else:
14
- return None
15
- except Exception as e:
16
- print(f"Error: {e}")
17
- return None
 
 
 
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}")