-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcrawl.py
251 lines (187 loc) · 7.74 KB
/
crawl.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import pinecone
from langchain.vectorstores import Pinecone
from langchain.text_splitter import NLTKTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.docstore.document import Document
from langchain import OpenAI
import tiktoken
from dotenv import load_dotenv
import requests
import re
import urllib.request
from bs4 import BeautifulSoup
from collections import deque
from html.parser import HTMLParser
from urllib.parse import urlparse
import os
import argparse
# Load the environment variables from the .env file
load_dotenv()
llm = OpenAI(temperature=0)
embeddings = OpenAIEmbeddings()
# Define the tokenizer
enc = tiktoken.get_encoding("gpt2")
# initialize pinecone
pinecone.init(api_key=os.getenv("PINECONE_API_KEY"),
environment=os.getenv("PINECONE_ENV"))
text_splitter = NLTKTextSplitter.from_tiktoken_encoder(
chunk_size=500, chunk_overlap=100)
# Regex pattern to match a URL
HTTP_URL_PATTERN = r'^http[s]*://.+'
parser = argparse.ArgumentParser()
parser.add_argument('--domain', help='The domain to crawl',
default='stripe.com')
parser.add_argument('--url', help='The base URL to crawl',
default='https://stripe.com/docs/')
parser.add_argument(
'--excludes', help='The URLs to exclude from the crawl', required=False)
args = parser.parse_args()
# Define root domain to crawl
domain = args.domain
base_url = args.url
excludes = args.excludes.split(",")
parsed_full_url = urlparse(base_url)
# reconstruct full url
base_url = parsed_full_url.scheme + "://" + \
parsed_full_url.netloc + parsed_full_url.path
full_url_path = parsed_full_url.path
# recontruct path from object
def get_url_from_object(parsed_url):
return parsed_url.scheme + "://" + parsed_url.netloc + parsed_url.path
def sanitize_text(text):
# Remove whitespace from the beginning and end of the text
text = text.strip()
# Remove all newlines from the text
text = text.replace("\n", " ")
# Remove all extra spaces from the text
text = " ".join(text.split())
# Remove all special characters from the text except punctuation but keep numbers
text = "".join([c for c in text if c.isalnum()
or c.isspace() or c in ".,?!:;'-"])
# Return the sanitized text
return text
# Create a class to parse the HTML and get the hyperlinks
class HyperlinkParser(HTMLParser):
def __init__(self):
super().__init__()
# Create a list to store the hyperlinks
self.hyperlinks = []
# Override the HTMLParser's handle_starttag method to get the hyperlinks
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
# If the tag is an anchor tag and it has an href attribute, add the href attribute to the list of hyperlinks
if tag == "a" and "href" in attrs:
self.hyperlinks.append(attrs["href"])
# Function to get the hyperlinks from a URL
def get_hyperlinks(url):
# Try to open the URL and read the HTML
try:
# Open the URL and read the HTML
with urllib.request.urlopen(url) as response:
# If the response is not HTML, return an empty list
if not response.info().get('Content-Type').startswith("text/html"):
return []
# Decode the HTML
html = response.read().decode('utf-8')
except Exception as e:
print(e)
return []
# Create the HTML Parser and then Parse the HTML to get hyperlinks
parser = HyperlinkParser()
parser.feed(html)
return parser.hyperlinks
# Function to get the hyperlinks from a URL that are within the same domain
def get_domain_hyperlinks(local_domain, url):
clean_links = []
for link in set(get_hyperlinks(url)):
clean_link = None
# If the link is a URL, check if it is within the same domain
if re.search(HTTP_URL_PATTERN, link):
# Parse the URL and check if the domain is the same
url_obj = urlparse(link)
if url_obj.netloc == local_domain:
clean_link = get_url_from_object(url_obj)
# If the link is not a URL, check if it is a relative link
else:
if link.startswith("/"):
link = link[1:]
elif link.startswith("#") or link.startswith("mailto:"):
continue
clean_link = "https://" + local_domain + "/" + link
if clean_link is not None:
# parse the link
clean_link = get_url_from_object(urlparse(clean_link))
# check if the link is in the excludes list and set it to None if it is
for exclude in excludes:
if exclude in clean_link:
clean_link = None
break
if clean_link is not None and base_url not in clean_link:
clean_link = None
if clean_link is not None:
if clean_link.endswith("/"):
clean_link = clean_link[:-1]
clean_links.append(clean_link)
# Return the list of hyperlinks that are within the same domain
return list(set(clean_links))
def crawl(url):
# Parse the URL and get the domain
local_domain = urlparse(url).netloc
# Create a queue to store the URLs to crawl
queue = deque([url])
# Create a set to store the URLs that have already been seen (no duplicates)
seen = set([url])
# Create a directory to store the text files
if not os.path.exists("text/"):
os.mkdir("text/")
if not os.path.exists("text/"+local_domain+"/"):
os.mkdir("text/" + local_domain + "/")
# While the queue is not empty, continue crawling
while queue:
# Get the next URL from the queue
url = queue.pop()
print(url) # for debugging and to see the progress
# Save text from the url to a <url>.txt file
with open('text/'+local_domain+'/'+url[8:].replace("/", "_") + ".txt", "w") as f:
# Get the text from the URL using BeautifulSoup
soup = BeautifulSoup(requests.get(url).text, "html.parser")
# Get the text but remove the tags
text = soup.get_text()
# If the crawler gets to a page that requires JavaScript, it will stop the crawl
if ("You need to enable JavaScript to run this app." in text):
print("Unable to parse page " + url +
" due to JavaScript being required")
text = sanitize_text(text)
text = f"{url}|{text}"
# Otherwise, write the text to the file in the text directory
f.write(text)
# Get the hyperlinks from the URL and add them to the queue
for link in get_domain_hyperlinks(local_domain, url):
if link not in seen:
queue.append(link)
seen.add(link)
if __name__ == '__main__':
print("Crawling...")
crawl(base_url)
# Create a list to store the text files
sources = []
# Get all the text files in the text directory
for file in os.listdir("text/" + domain + "/"):
# Open the file and read the text
with open("text/" + domain + "/" + file, "r") as f:
text = f.read()
url = text.split("|")[0]
text = text.split("|")[1]
sources.append((url, text))
print("Creating chunks from the webpages...")
docs = []
for (url, source) in sources:
texts = text_splitter.split_text(source)
docs.extend(
[Document(page_content=t, metadata={"url": url}) for t in texts])
print("Saving sources in the Pinecone index...")
index_name = os.getenv("PINECONE_INDEX_NAME")
namespace = os.getenv("PINECONE_INDEX_NAME")
docsearch = Pinecone.from_documents(
docs, embeddings, index_name=index_name, namespace=namespace)
print("Done! ✅")