2012-04-05 8 views
3

私は非常に単純なWebクローラーを作成し、'robots.txt'ファイルを解析しようとしています。私はrobotparserモジュールを標準ライブラリに見つけました。これはまさにこれを行うはずです。私はPython 2.7.2を使用しています。残念ながら、私のコードは'robots.txt'ファイルを正しく読み込まないため、なぜその理由が分かりません。ここでPython robotparserモジュールは 'robots.txt'をロードしません

は、私のコードの関連するスニペットです:

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 

ここpage解析されBeautifulSoup対象とurlが文字列として格納されたURLです。パーサは、指定されたURLにあるファイルではなく、空白の'robots.txt'ファイルを読み込み、すべてcan_fetch()クエリにTrueを返します。 URLを開いていないか、テキストファイルを読み込めなかったようです。

私はインタラクティブなインタプリタでも試してみました。これは、documentationページと同じ構文を使用して行われます。

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 

>>> 

ラインprint rp'robots.txt'ファイルの内容を印刷する必要がありますが、それは空白を返します。 theseexamplesは、書かれているとおり完全に正常に動作しますが、自分のURLを試してみると失敗します。私はPythonの初心者です。何がうまくいかないのか分かりません。私が知る限り、私はドキュメンテーションとサンプルと同じ方法でモジュールを使用しています。助けてくれてありがとう!

UPDATE 1:ここでははケースにprint rp'robots.txt'が読み込まれたかどうかを確認するための良い方法ではありませんでした、インタプリタから数行以上あるpathhost、およびurl属性が正しいですが、エントリーから。 'robots.txt'は、まだ読まれていない

>>> 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:私は'robots.txt'ファイルを解析するためにthis external libraryを使用することによって、この問題を解決しています。 (しかし私は元の質問に答えなかった!)端末でもう少し時間を過ごした後、robotparserSitemapのような'robots.txt'仕様への特定の追加を処理できず、空白行に問題がある。ファイルから読み込みます。 Stack OverflowやPython.org(ただし、Google、YouTube、オリジナルのUdacityファイルは含まれません)には、Sitemapステートメントと空白行が含まれています。私より賢い人がこれを確かめるか説明することができたら、私はまだそれを感謝します!

+0

ところで、私は関連する何かを省いた場合のために、このスニペットを[here](ここ[https://github.com/ecmendenhall/DaveDaveFind/blob/master/crawler/udacity_crawler.py])の文脈で見ることができます。 – ecmendenhall

+0

line print rpは 'robots.txt'ファイルの内容を出力するはずです。 – Hoff

+0

確かに。私がリンクしている外部の例を使用した場合、これは動作しています。その場合、私はインタプリタからのいくつかの情報で質問を更新しました。 URL属性はすべて正しく見えますが、 'entries'は空のリストです。 – ecmendenhall

答えて

2

私はこの外部ライブラリを使用して 'robots.txt'ファイルを解析することでこの問題を解決しました。 (しかし私は元の質問に答えなかった!)端末でもう少し時間を費やした後、robparserはSitemapのような 'robots.txt'仕様への特定の追加を処理できず、空白行に問題がある。ファイルから読み込みます。 Stack OverflowとPython.org(ただし、Google、YouTube、オリジナルのUdacityファイルは含まれません)には、サイトマップステートメントや空白行が含まれています。私より賢い人がこれを確かめるか説明することができたら、私はまだそれを感謝します!

0

溶液ここreppyモジュール

pip install reppy 

を使用することができるいくつかの例です。

In [1]: import reppy 

In [2]: x = reppy.fetch("http://google.com/robots.txt") 

In [3]: x.atts 
Out[3]: 
{'agents': {'*': <reppy.agent at 0x1fd9610>}, 
'sitemaps': ['http://www.gstatic.com/culturalinstitute/sitemaps/www_google_com_culturalinstitute/sitemap-index.xml', 
    'http://www.google.com/hostednews/sitemap_index.xml', 
    'http://www.google.com/sitemaps_webmasters.xml', 
    'http://www.google.com/ventures/sitemap_ventures.xml', 
    'http://www.gstatic.com/dictionary/static/sitemaps/sitemap_index.xml', 
    'http://www.gstatic.com/earth/gallery/sitemaps/sitemap.xml', 
    'http://www.gstatic.com/s2/sitemaps/profiles-sitemap.xml', 
    'http://www.gstatic.com/trends/websites/sitemaps/sitemapindex.xml']} 

In [4]: x.allowed("/catalogs/about", "My_crawler") # Should return True, since it's allowed. 
Out[4]: True 

In [5]: x.allowed("/catalogs", "My_crawler") # Should return False, since it's not allowed. 
Out[5]: False 

In [7]: x.allowed("/catalogs/p?", "My_crawler") # Should return True, since it's allowed. 
Out[7]: True 

In [8]: x.refresh() # Refresh robots.txt, perhaps a magic change? 

In [9]: x.ttl 
Out[9]: 3721.3556718826294 

Voila!

関連する問題