@@ -63,29 +63,33 @@ def resolve_xpath(xpath: str, url: str) -> str:
63
63
if not is_valid_xpath (xpath ):
64
64
raise ValueError (f"The xpath, { xpath } , is not valid." )
65
65
66
- response = requests .get (url )
67
-
68
- if response .ok :
69
- tree = html .fromstring (response .content )
70
- values = tree .xpath (xpath )
71
-
72
- if len (values ) == 1 :
73
- if isinstance (values [0 ], str ):
74
- text_content = values [0 ]
75
- else :
76
- text_content = values [0 ].text
77
-
78
- if text_content :
79
- text_content = clean_text (text_content )
80
- return text_content
66
+ try :
67
+ response = requests .get (url )
68
+
69
+ if response .ok :
70
+ tree = html .fromstring (response .content )
71
+ values = tree .xpath (xpath )
72
+
73
+ if len (values ) == 1 :
74
+ if isinstance (values [0 ], str ):
75
+ text_content = values [0 ]
76
+ else :
77
+ text_content = values [0 ].text
78
+
79
+ if text_content :
80
+ text_content = clean_text (text_content )
81
+ return text_content
82
+ else :
83
+ raise ValueError (f"The element at the xpath, { xpath } , does not contain any text content." )
84
+ elif len (values ) > 1 :
85
+ raise ValueError (f"More than one element found for the xpath, { xpath } " )
81
86
else :
82
- raise ValueError (f"The element at the xpath, { xpath } , does not contain any text content." )
83
- elif len (values ) > 1 :
84
- raise ValueError (f"More than one element found for the xpath, { xpath } " )
87
+ raise ValueError (f"No element found for the xpath, { xpath } " )
85
88
else :
86
- raise ValueError (f"No element found for the xpath, { xpath } " )
87
- else :
88
- raise ValueError (f"Failed to retrieve the { url } . Status code: { response .status_code } " )
89
+ raise ValueError (f"Failed to retrieve the { url } . Status code: { response .status_code } " )
90
+
91
+ except requests .RequestException as e :
92
+ raise ValueError (f"Network error while accessing { url } : { str (e )} " )
89
93
90
94
91
95
def parse_title (input_string : str ) -> list [tuple [str , str ]]:
0 commit comments