This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.py
89 lines (79 loc) · 2.44 KB
/
crawler.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
import os
import sys
import re
from string import *
import nltk
import urllib
from os import listdir
import urlparse
class urlSite:
urlsRoot = os.getcwd() + '/crawler_sources/'
corpusRoot = os.getcwd() + "/corpus/"
def __init__(self,url,patterns):
self.url=url
self.patterns=patterns
self.content=''
self.textAdresses=[]
self.hostname = urlparse.urlparse(url).hostname
# Loads all url files from this directory
# Returns an array of urlSite objects
# if process is true, then for each urlSite, it will do all operations
def fromDir(dir, process = False):
urls = []
for f in listdir(dir):
if re.match('.+\.txt$',f):
url = urlSite.fromFile(dir+f, process)
if url:
urls.append(url)
return urls
fromDir = staticmethod(fromDir)
# Loads a url from the given object
# Returns urlSite
# If process is true, it downloads root content, extracts addresses and download pages
def fromFile(fileName, process = False):
url = None
pats = []
for line in map(lambda x:x.replace('\r','').replace('\n',''),open(fileName).readlines()):
m = re.match('^root=(.+)$',line)
if m:
url=m.group(1)
continue
m = re.match('^1 pattern(.+)$',line)
if m:
pats.append(m.group(1))
continue
if url:
us = urlSite(url,pats)
if process:
us.process()
return us
fromFile = staticmethod(fromFile)
def downloadRootContent(self):
self.content = urllib.urlopen(self.url).read()
def extractAddressesFromRootContent(self):
for pat in self.patterns:
self.textAdresses += re.findall(pat,self.content, re.DOTALL)
def downloadAddresses(self):
urlDir = self.corpusRoot + self.hostname + '/'
if not os.path.exists(urlDir):
os.makedirs(urlDir)
urlsFile = open(urlDir + 'all.urls', 'a')
for ad in self.textAdresses:
f = urllib.urlopen(ad)
html = f.read()
i = 1
fileName = lambda: urlDir + str(i) + ".html"
while os.path.exists(fileName()):
i = i+1
text_file = open(fileName(), "w")
text_file.write(html)
text_file.close()
urlsFile.write('{0} {1}\n'.format(fileName(), ad))
urlsFile.close()
def process(self):
self.downloadRootContent()
self.extractAddressesFromRootContent()
self.downloadAddresses()
#urls = urlSite.fromDir(urlSite.urlsRoot, False)
#urls[0].process()
urls = urlSite.fromDir(urlSite.urlsRoot, True)