Skip to content

Fix spider #294

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

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Check robots.txt before many HEAD requests
  • Loading branch information
jayvdb committed Jul 28, 2016
commit 0116f9997614bb415c7e31bab49957e3059929f3
21 changes: 16 additions & 5 deletions utils/spider.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ def updateURLs(self, tree):
newUrls.add(urllib_parse.urlunsplit(splitURL))
urls = newUrls

toVisit = self.check_robots(urls)
toVisit = self.check_headers(toVisit)

self.visitedURLs.update(urls)
self.unvisitedURLs.update(toVisit)

def check_headers(self, urls):
responseHeaders = {}
# Now we want to find the content types of the links we haven't visited
for url in urls:
Expand All @@ -128,8 +135,13 @@ def updateURLs(self, tree):
'html' in responseHeaders[url].get('content-type', '') and
responseHeaders[url]['status'] == "200"])

return toVisit

def check_robots(self, urls):
# Now check we are allowed to spider the page
for url in list(toVisit):
toVisit = list(urls)

for url in toVisit:
robotURL = list(urllib_parse.urlsplit(url)[:2])
robotURL.extend(["robots.txt", "", ""])
robotURL = urllib_parse.urlunsplit(robotURL)
Expand All @@ -138,15 +150,14 @@ def updateURLs(self, tree):
self.robotParser.read()
except Exception as e:
print('Failed to read {0}: {1}'.format(robotURL, e), file=sys.stderr)
toVisit.remove(url)
urls.remove(url)
continue

if not self.robotParser.can_fetch("*", url):
print('{0} rejects {1}'.format(robotURL, url), file=sys.stderr)
toVisit.remove(url)
urls.remove(url)

self.visitedURLs.update(urls)
self.unvisitedURLs.update(toVisit)
return urls


def main():
Expand Down