Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python机器人解析器模块赢得';t负载';robots.txt';_Python_Python 2.7_Robots.txt_Python Module - Fatal编程技术网

Python机器人解析器模块赢得';t负载';robots.txt';

Python机器人解析器模块赢得';t负载';robots.txt';,python,python-2.7,robots.txt,python-module,Python,Python 2.7,Robots.txt,Python Module,我正在编写一个非常简单的网络爬虫程序,并试图解析'robots.txt'文件。我在标准库中找到了robotparser模块,该模块正好可以做到这一点。我正在使用Python 2.7.2。不幸的是,我的代码无法正确加载'robots.txt'文件,我也不知道为什么 以下是我的代码的相关片段: from urlparse import urlparse, urljoin import robotparser def get_all_links(page, url): links = []

我正在编写一个非常简单的网络爬虫程序,并试图解析
'robots.txt'
文件。我在标准库中找到了
robotparser
模块,该模块正好可以做到这一点。我正在使用Python 2.7.2。不幸的是,我的代码无法正确加载
'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
是一个已解析的对象,
url
是一个存储为字符串的url。解析器读入一个空白的
'robots.txt'
文件,而不是指定URL处的文件,并将
True
返回给所有
can\u fetch()
查询。看起来不是没有打开URL就是无法读取文本文件

我也在交互式口译员中试过。这就是发生的情况,使用与页面相同的语法

print rp
应打印
'robots.txt'
文件的内容,但返回空白。更令人沮丧的是,这两种方法都写得很好,但当我尝试自己的URL时失败了。我对Python非常陌生,不知道出了什么问题。据我所知,我正在以与文档和示例相同的方式使用该模块。谢谢你的帮助

更新1:这里还有几行来自解释器的文字,以防
打印rp
不是检查是否读入
'robots.txt'
的好方法。
路径
主机
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'
>>> 
>>rp
>>>主任(rp)
[“文件”、“初始化”、“模块”、“修改”、“mtime”、“解析”、“路径”、“读取”、“设置url”、“url”]
>>>rp.path
“/robots.txt”
>>>rp.host
“www.udacity-forums.com”
>>>rp.条目
[]
>>>rp.url
'http://www.udacity-forums.com/robots.txt'
>>> 

更新2:我通过使用解析
'robots.txt'
文件解决了这个问题。(但我还没有回答原来的问题!)在终端上花了一段时间后,我最好的猜测是
robotparser
无法处理添加到
'robots.txt'
规范中的某些内容,如
Sitemap
,并且无法处理空行。它将读取Stack Overflow和Python.org等文件,但不会读取Google、YouTube或我的原始Udacity文件,其中包括
Sitemap
语句和空行。如果有比我聪明的人能证实或解释这一点,我还是会很感激的

我通过使用这个外部库解析“robots.txt”文件解决了这个问题。(但我还没有回答最初的问题!)在终端上花费了更多的时间后,我最好的猜测是robotparser无法处理“robots.txt”规范中的某些附加内容,如Sitemap,并且无法处理空行。它将读取Stack Overflow和Python.org等文件,但不会读取Google、YouTube或我的原始Udacity文件,其中包括站点地图语句和空行。如果有比我聪明的人能证实或解释这一点,我还是会很感激的

解决方案可以使用模块

这里有几个例子

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
[1]中的
:导入reppy
在[2]中:x=reppy.fetch(“http://google.com/robots.txt")
在[3]中:x.atts
出[3]:
{'agents':{'*':},
“站点地图”:['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']}
在[4]:x.allowed(“/catalogs/about”,“My_crawler”)#应该返回True,因为它是允许的。
Out[4]:正确
在[5]:x.allowed(“/catalogs”,“My_crawler”)#应该返回False,因为它是不允许的。
Out[5]:假
在[7]:x.allowed(“/catalogs/p?”,“My_crawler”)#应该返回True,因为它是允许的。
Out[7]:对
在[8]:x.refresh()#refresh robots.txt中,可能是一个神奇的变化?
In[9]:x.ttl
Out[9]:3721.3556718826294

顺便说一句,你可以在上下文中看到这个片段,以防我遗漏了一些相关内容。print rp行应该打印“robots.txt”文件的内容-你确定吗?非常确定。当我使用我链接的外部示例时,它就是这样表现的。以防万一,我用口译员提供的更多信息更新了我的问题。URL属性看起来都不错,但是
条目
是一个空列表。我也遇到了同样的问题,我尝试使用您提到的库()进行解析,当我尝试提取时(“*”,“/catalogs/p?”)返回False,即使这是允许的。我在这里有点怀疑。有什么线索吗?
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