Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,23 +31,23 @@ def my_custom_tool(arg1:str)-> list[str]: #it's import to specify the return typ
|
|
| 31 |
|
| 32 |
try:
|
| 33 |
response = requests.get(url, headers=headers)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
except requests.exceptions.RequestException as e:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
except Exception as e:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
|
| 53 |
@tool
|
|
|
|
| 31 |
|
| 32 |
try:
|
| 33 |
response = requests.get(url, headers=headers)
|
| 34 |
+
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
|
| 35 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
| 36 |
+
results = soup.find_all("li", class_="booklink")
|
| 37 |
+
urls = []
|
| 38 |
+
for result in results[:10]: # grabs the top 10
|
| 39 |
+
link = result.find("a", class_="link")
|
| 40 |
+
if link:
|
| 41 |
+
full_url = "https://www.gutenberg.org" + link["href"]
|
| 42 |
+
urls.append(full_url)
|
| 43 |
+
return urls
|
| 44 |
|
| 45 |
except requests.exceptions.RequestException as e:
|
| 46 |
+
print(f"Error: {e}")
|
| 47 |
+
return None
|
| 48 |
except Exception as e:
|
| 49 |
+
print(f"An unexpected error occurred: {e}")
|
| 50 |
+
return None
|
| 51 |
|
| 52 |
|
| 53 |
@tool
|