Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update crawling object of gather_links() from a tag to entire response html #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions spider.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Spider:

def __init__(self, project_name, base_url, domain_name):
Spider.project_name = project_name
Spider.base_url = base_url
Spider.base_url = base_url if base_url[-1] != '/' else base_url[:-1] # erase last /
Spider.domain_name = domain_name
Spider.queue_file = Spider.project_name + '/queue.txt'
Spider.crawled_file = Spider.project_name + '/crawled.txt'
Expand Down Expand Up @@ -45,18 +45,21 @@ def crawl_page(thread_name, page_url):
# Converts raw response data into readable information and checks for proper html formatting
@staticmethod
def gather_links(page_url):
import re
html_string = ''
try:
response = urlopen(page_url)
if 'text/html' in response.getheader('Content-Type'):
html_bytes = response.read()
html_string = html_bytes.decode("utf-8")
finder = LinkFinder(Spider.base_url, page_url)
finder.feed(html_string)

pattern = "(\"|')(\/[\w\d?\/&=#.!:_-]{1,})(\"|')"
matches = re.findall(pattern, html_string)
result_urls = [ Spider.base_url + match[1] for match in matches]
return result_urls
except Exception as e:
print(str(e))
return set()
return finder.page_links()

# Saves queue data to project files
@staticmethod
Expand Down