-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawler.py
More file actions
72 lines (58 loc) · 2.2 KB
/
Copy pathcrawler.py
File metadata and controls
72 lines (58 loc) · 2.2 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
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
import requests
from bs4 import BeautifulSoup as bs
from whoosh.index import create_in
from whoosh.index import open_dir
from whoosh.fields import *
from whoosh.qparser import QueryParser
# For setup we use simple presets they are inelegant but they do the job
prefix = "https://vm009.rz.uos.de/crawl/"
start_url = prefix + "index.html"
agenda = [start_url]
# We create the schema in which woosh will store data
schema = Schema(
url=TEXT(stored=True),
title=TEXT(stored=True),
body=KEYWORD,
excerpt=TEXT(stored=True),
)
ix = create_in("indexdir", schema)
# Predefine saver to help with code legibility
def saver(soup, index, url):
# Extract and cut to size various parts of the soup
title = soup.html.title.text
body = soup.html.p.text
writer = index.writer()
excerpt = soup.html.p.text
excerpt = excerpt[0:50]
# Save the extracted parts
writer.add_document(url=url, title=title, body=body + " " + title, excerpt=excerpt)
writer.commit()
return
# Loop for as long as there is something on the agenda
while agenda:
# Grab the top object from the agenda
url = agenda.pop()
# Create a requests object
r = requests.get(url)
# Check wheter the target page is reachable
if r.status_code == 200:
# Extracts the soup from the page and saves it into our woosh library
soup = bs(r.text, features="lxml")
saver(soup, ix, url)
# look for further links to crawl
for link in soup.find_all("a"):
string = link.get("href")
# ensusure found links are html links
if ".html" in string:
# ensures that links with and without the proper prefix get added
if prefix in string:
newurl = string
else:
newurl = prefix + string
# Checks wheter the link already as been visited or is alredy in agenda
query = QueryParser("url", ix.schema).parse(string)
with ix.searcher() as searcher:
results = searcher.search(query)
if len(results) == 0:
if (newurl in agenda) == False:
agenda.append(newurl)