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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check robot.txt and ai.txt #13

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
55 changes: 55 additions & 0 deletions lazynlp/robots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import re
import requests

regex_url = r"https?:\/\/(www)?[^\/]*"
regex_dis = r"Disallow:\s*([^\n]*)"


def find_match(url):
"""
Extracts base url from a given url
returns status 0 for success and -1 for fail
"""
matches = re.search(regex_url, url)
if matches:
base_url = matches.group()
return 0, base_url
return -1, ''


def generate_robot_url(url):
"""
returns the url for robots.txt file
returns status 0 for success and -1 for fail
returns the base url as the third parameter
"""
success, baseurl = find_match(url)
if success != 0:
return -1,''
robots_url = baseurl + '/robots.txt'
return 0,robots_url,baseurl

def process_robot_string_to_regex(robot):
"""
converts disallowed url to regex string and returns it
"""
regex = re.sub(r"\*", "[^/]*", robot, 0, re.IGNORECASE)
return regex

def read_disallows(url):
"""
returns baseurl and a list of Pattern objects for disallowed urls
you can map the urls based on the baseurl and then check every url corresponding to the base url
with the pattern objects in this list to see if the given url is allowed by robots.txt for crawling
"""
success, robots_url,baseurl = generate_robot_url(url)
if success != 0:
return []
disallowed = []
data = requests.get(robots_url).text
matches = re.finditer(regex_dis, data, re.MULTILINE | re.IGNORECASE)
for matchNum, match in enumerate(matches, start=1):
url = match.group(1)
outp = process_robot_string_to_regex(url)
disallowed.append(re.compile(outp))
return baseurl,disallowed