Spaces:
Runtime error
Runtime error
gamingflexer
commited on
Commit
·
d6f0606
1
Parent(s):
59a1246
browser tool added
Browse files- src/scrapper/tools.py +21 -0
src/scrapper/tools.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import requests
|
3 |
+
from langchain.tools import tool
|
4 |
+
from unstructured.partition.html import partition_html
|
5 |
+
|
6 |
+
class BrowserTools():
|
7 |
+
@tool("Scrape website content")
|
8 |
+
def scrape_website(website):
|
9 |
+
"""Useful to scrape a website content"""
|
10 |
+
url = f"https://chrome.browserless.io/content?token={config('BROWSERLESS_API_KEY')}"
|
11 |
+
payload = json.dumps({"url": website})
|
12 |
+
headers = {
|
13 |
+
'cache-control': 'no-cache',
|
14 |
+
'content-type': 'application/json'
|
15 |
+
}
|
16 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
17 |
+
elements = partition_html(text=response.text)
|
18 |
+
content = "\n\n".join([str(el) for el in elements])
|
19 |
+
|
20 |
+
# Return only the first 5k characters
|
21 |
+
return content[:5000]
|