Compare commits
2 Commits
759f965e95
...
c436016e0c
| Author | SHA1 | Date | |
|---|---|---|---|
| c436016e0c | |||
| 03554fde80 |
@@ -4,7 +4,7 @@ Need a docstring.
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
from utils.helpers import (UrlPool, WebPage, sanitise_url, qualify_url)
|
from utils.helpers import (UrlPool, WebPage, sanitise_url)
|
||||||
|
|
||||||
def init_crawler(base_url=None):
|
def init_crawler(base_url=None):
|
||||||
'''
|
'''
|
||||||
|
|||||||
@@ -41,15 +41,24 @@ class WebPage(object):
|
|||||||
|
|
||||||
|
|
||||||
def get_source(self):
|
def get_source(self):
|
||||||
|
'''
|
||||||
|
Retrieve a page's source.
|
||||||
|
'''
|
||||||
|
|
||||||
request = urllib.request.Request(self.url, headers=self.headers)
|
request = urllib.request.Request(self.url, headers=self.headers)
|
||||||
page = urllib.request.urlopen(request)
|
page = urllib.request.urlopen(request)
|
||||||
self.source = page.read()
|
self.source = page.read()
|
||||||
|
|
||||||
|
|
||||||
def find_links(self):
|
def find_links(self):
|
||||||
|
'''
|
||||||
|
Find all URLs on a page and ensure they are absolute. If they are
|
||||||
|
relative then they will be appended to the base URL.
|
||||||
|
'''
|
||||||
|
hrefs = set()
|
||||||
|
|
||||||
soup = BeautifulSoup(self.source, 'html.parser')
|
soup = BeautifulSoup(self.source, 'html.parser')
|
||||||
links = soup.find_all('a')
|
links = soup.find_all('a')
|
||||||
hrefs = set()
|
|
||||||
|
|
||||||
for link in links:
|
for link in links:
|
||||||
if link['href'].startswith('/'):
|
if link['href'].startswith('/'):
|
||||||
@@ -61,6 +70,11 @@ class WebPage(object):
|
|||||||
|
|
||||||
|
|
||||||
def parse_urls(self):
|
def parse_urls(self):
|
||||||
|
'''
|
||||||
|
Iterate through the list of discovered URLs and add them to the
|
||||||
|
pool if they start with the base URL.
|
||||||
|
'''
|
||||||
|
|
||||||
self.urls_to_crawl = set()
|
self.urls_to_crawl = set()
|
||||||
for url in self.discovered_hrefs:
|
for url in self.discovered_hrefs:
|
||||||
if url.startswith(self.url):
|
if url.startswith(self.url):
|
||||||
@@ -104,16 +118,3 @@ def sanitise_url(url):
|
|||||||
base_url = "".join([default_proto, delim, split_url.path])
|
base_url = "".join([default_proto, delim, split_url.path])
|
||||||
|
|
||||||
return base_url
|
return base_url
|
||||||
|
|
||||||
|
|
||||||
def qualify_url(base_url=None, url=None):
|
|
||||||
'''
|
|
||||||
Ensure any URLs discovered are absolute. If relative,
|
|
||||||
they will be appended to the base URL. Returns an
|
|
||||||
absolute URL as a string.
|
|
||||||
'''
|
|
||||||
|
|
||||||
if url.startswith('/'):
|
|
||||||
return urljoin(base_url, url)
|
|
||||||
if url.startswith(base_url):
|
|
||||||
return url
|
|
||||||
|
|||||||
Reference in New Issue
Block a user