Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7819423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:56:51+00:00 2026-06-02T06:56:51+00:00

I’m writing a very simple web crawler and trying to parse ‘robots.txt’ files. I

  • 0

I’m writing a very simple web crawler and trying to parse 'robots.txt' files. I found the robotparser module in the standard library, which should do exactly this. I’m using Python 2.7.2. Unfortunately, my code won’t load the 'robots.txt' files correctly, and I can’t figure out why.

Here is the relevant snippet of my code:

from urlparse import urlparse, urljoin
import robotparser

def get_all_links(page, url):
    links = []
    page_url = urlparse(url)
    base = page_url[0] + '://' + page_url[1]
    robots_url = urljoin(base, '/robots.txt')
    rp = robotparser.RobotFileParser()
    rp.set_url(robots_url)
    rp.read()
    for link in page.find_all('a'):
        link_url = link.get('href')
        print "Found a link: ", link_url
        if not rp.can_fetch('*', link_url):
            print "Page off limits!" 
            pass

Here page is a parsed BeautifulSoup object and url is a URL stored as a string. The parser reads in a blank 'robots.txt' file, instead of the one at the specified URL, and returns True to all can_fetch() queries. It looks like it’s either not opening the URL or failing to read the text file.

I’ve tried it in the interactive interpreter, too. This is what happens, using the same syntax as the documentation page.

Python 2.7.2 (default, Aug 18 2011, 18:04:39) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import robotparser
>>> url = 'http://www.udacity-forums.com/robots.txt'
>>> rp = robotparser.RobotFileParser()
>>> rp.set_url(url)
>>> rp.read()
>>> print rp

>>> 

The line print rp should print the contents of the 'robots.txt' file, but it returns blank. Even more frustrating, these examples both work perfectly fine as written, but fail when I try my own URL. I’m pretty new to Python, and I can’t figure out what’s going wrong. As far as I can tell, I’m using the module in the same way as the documentation and examples. Thanks for any help!

UPDATE 1: Here are a few more lines from the interpreter, in case print rp was not a good method to check if 'robots.txt' was read in. The path, host, and url attributes are correct, but the entries from 'robots.txt' have still not been read in.

>>> rp
<robotparser.RobotFileParser instance at 0x1004debd8>
>>> dir(rp)
['__doc__', '__init__', '__module__', '__str__', '_add_entry', 'allow_all', 'can_fetch', 'default_entry', 'disallow_all', 'entries', 'errcode', 'host', 'last_checked', 'modified', 'mtime', 'parse', 'path', 'read', 'set_url', 'url']
>>> rp.path
'/robots.txt'
>>> rp.host
'www.udacity-forums.com'
>>> rp.entries
[]
>>> rp.url
'http://www.udacity-forums.com/robots.txt'
>>> 

UPDATE 2: I have solved this problem by using this external library to parse 'robots.txt' files. (But I haven’t answered the original question!) After spending some more time in the terminal, my best guess is that robotparser cannot handle certain additions to the 'robots.txt' spec, like Sitemap, and has trouble with blank lines. It will read in files from, e.g. Stack Overflow and Python.org, but not Google, YouTube, or my original Udacity file, which include Sitemap statements and blank lines. I’d still appreciate it if someone smarter than me could confirm or explain this!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:56:53+00:00Added an answer on June 2, 2026 at 6:56 am

    I have solved this problem by using this external library to parse ‘robots.txt’ files. (But I haven’t answered the original question!) After spending some more time in the terminal, my best guess is that robotparser cannot handle certain additions to the ‘robots.txt’ spec, like Sitemap, and has trouble with blank lines. It will read in files from, e.g. Stack Overflow and Python.org, but not Google, YouTube, or my original Udacity file, which include Sitemap statements and blank lines. I’d still appreciate it if someone smarter than me could confirm or explain this!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.