-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
43 lines (35 loc) · 1.08 KB
/
Copy pathtools.py
File metadata and controls
43 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import html2text
import requests
from dotenv import load_dotenv
from serpapi import BingSearch
load_dotenv()
def get_organic_search_results(query):
params = {
"q": query,
"cc": "US",
"api_key": os.getenv("SERPAPI_API_KEY"),
}
search = BingSearch(params)
results = search.get_dict()["organic_results"]
txt = f"# Website results for the query: {query}\n\n"
for r in results:
txt += f"{r['position']}. [{r['title']}]({r['link']})"
if "snippet" in r:
txt += f" - {r['snippet']}"
txt += "\n"
return txt
def get_markdown_from_url(url):
# Step 1 & 2: Download HTML from URL
response = requests.get(url)
html = response.text
# Step 3 & 4: Convert HTML to Markdown
text_maker = html2text.HTML2Text()
text_maker.ignore_links = False
markdown = text_maker.handle(html)
return markdown
if __name__ == "__main__":
result = get_organic_search_results("vegetarian resturants in san francisco")
# usage
url = "https://example.com"
print(get_markdown_from_url(url))