robots.txt 和 robots meta 标签应用详解
搜索引擎都有自己的“机器人”(robots、bot),通常也叫做“蜘蛛“和“爬虫”(spider),并通过这些 robots 在网络上沿着网页上的链接(一般是 http 和 src 链接)不断抓取资料建立自己的数据库。
对于网站管理者和内容提供者来说,有时候会有一些站点内容,不希望被 robots 抓取而公开。为了解决这个问题,robots 开发界提供了两个方法:一个是 robots.txt,另一个是 robots meta 标签。下面将对这两种方法进行详细的介绍。
一、robots.txt
1. 什么是 robots.txt?
robots.txt 是一个纯文本文件,通过在这个文件中声明该网站中不想被 robots 访问的部分,这样,该网站的部分或全部内容就可以不被搜索引擎收录了,或者指定搜索引擎只收录指定的内容。
当一个搜索机器人访问一个站点时,它会首先检查该站点根目录下是否存在 robots.txt,如果找到,搜索机器人就会按照该文件中的内容来确定访问的范围,如果该文件不存在,那么搜索机器人就沿着链接抓取。
robots.txt 必须放置在一个站点的根目录下,而且文件名必须全部小写。
如 www.i0554.com,根目录下的 robots 就是 http://www.i0554.com/robots.txt
2. robots.txt 的语法
“robots.txt”文件包含一条或更多的记录,这些记录通过空行分开(以 CR,CR/NL, or NL 作为结束符)。在该文件中可以使用#进行注解。该文件中的记录通常以一行或多行 User-agent 开始,后面加上若干 Disallow 行。
User-agent:
该项的值用于描述搜索引擎 robot 的名字,在“robots.txt”文件中,如果有多条 User-agent 记录说明有多个 robot 会受到该协议的限制,对该文件来说,至少要有一条 User-agent 记录。如果该项的值设为*,则该协议对任何机器人均有效,在“robots.txt”文件中, “User-agent:*“这样的记录只能有一条。
Disallow :
该项的值用于描述不希望被访问到的一个 URL,这个 URL 可以是一条完整的路径,也可以是部分的,任何以 Disallow 开头的 URL 均不会被 robot 访问到。例如“Disallow: /help”对/help.html 和/help/index.html 都不允许搜索引擎访问,而“Disallow: /help/“则允许 robot 访问/help.html,而不能访问/help/index.html。
任何一条 Disallow 记录为空,说明该网站的所有部分都允许被访问,在”/robots.txt“文件中,至少要有一条 Disallow 记录。如果 “/robots.txt”是一个空文件,则对于所有的搜索引擎 robot,该网站都是开放的。
下面是一些 robots.txt 基本的用法:
1)禁止所有搜索引擎访问网站的任何部分:
User-agent: *Disallow: /2)允许所有的 robot 访问
User-agent: *Disallow:或者也可以建一个空文件 “/robots.txt” file
3)禁止所有搜索引擎访问网站的几个部分(下例中的 cgi-bin、tmp、private 目录)
User-agent: *Disallow: /cgi-bin/Disallow: /tmp/Disallow: /private/4)禁止某个搜索引擎的访问(下例中的 BadBot,有些流氓是不管这些的)
User-agent: BadBotDisallow: /5)只允许某个搜索引擎的访问(下例中的 WebCrawler)
User-agent: WebCrawler
Disallow:
3、常见搜索引擎机器人 robots 名字
快速链接:搜索引擎蜘蛛爬虫一览
4. 常见 robots.txt 错误
1)颠倒了顺序,错误写成
User-agent: *Disallow: GoogleBot正确的应该是:
User-agent: GoogleBotDisallow: *2)把多个禁止命令放在一行中,错误写成
Disallow: /css/ /cgi-bin/ /images/
正确的应该是
Disallow: /css/
Disallow: /cgi-bin/
Disallow: /images/
3)404 重定向到另外一个页面
当 Robot 访问很多没有设置 robots.txt 文件的站点时,会被自动 404 重定向到另外一个页面。这时 Robot 常常会以处理 robots.txt 文件的方式处理这个 Html 页面文件。虽然一般这样没有什么问题,但是最好能放一个空白的 robots.txt 文件在站点根目录下。
4)语法中只有 Disallow,没有 Allow!错误写成
User-agent: Baiduspider
Disallow: /john/
allow: /jane/
5)忘记了斜杠/,错误写成
User-agent: Baiduspider
Disallow: css
正确的应该是
User-agent: Baiduspider
Disallow: /css/
二、robots meta 标签
1. 什么是 robots meta 标签
robots.txt 文件主要是限制整个站点或者目录的搜索引擎访问情况,而 robots meta 标签则主要是针对一个个具体的页面。和其他的 meta 标签(如使用的语言、页面的描述、关键词等)一样,robots meta 标签也是放在页面的
中,专门用来告诉搜索引擎 robots 如何抓取该页的内容。具体的形式类似(见黑体部分):<html> <head> <title>xxx</title> <meta name="robots" content="index,follow" /> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="keywords" content="xx" /> <meta name="description" content="xx" /> <link rel="stylesheet" href="/public/css.css" type="text/css" /> </head> <body> … </body></html>2、robots meta 标签的写法
robots meta 标签中没有大小写之分,name=“robots”表示所有的搜索引擎,可以针对某个具体搜索引擎写为 name=“BaiduSpider”。content 部分有四个指令选项:index、noindex、follow、nofollow,指令间以小写逗号“,“分隔。
index 指令告诉搜索机器人抓取该页面;follow 指令表示搜索机器人可以沿着该页面上的链接继续抓取下去;robots meta 标签的缺省值是 index 和 follow。
一共有四种组合:
<meta name="robots" content="index,follow" /><meta name="robots" content="noindex,follow" /><meta name="robots" content="index,nofollow" /><meta name="robots" content="noindex,nofollow" />其中 <meta name="robots" content="index,follow" /> 可以简写为 <meta name="robots" content="all" />;<meta name="robots" content="noindex,nofollow" /> 可以简写为 <meta name="robots" content="none" />。
需要注意的是:上述的 robots.txt 和 robots meta 标签限制搜索引擎机器人(robots)抓取站点内容的办法只是一种规则,需要搜索引擎机器人的配合才行,并不是每个 robots 都遵守的。
绝大多数的搜索引擎机器人都遵守 robots.txt 的规则,而对于 robots meta 标签,目前支持的并不多,但是正在逐渐增加,如 Google 就完全支持,而且 Google 还增加了一个指令“archive”,可以限制 Google 是否保留网页快照。例如:
<meta name="googlebot" content="index,follow,noarchive" />表示抓取该站点中页面并沿着页面中链接抓取,但是不在 Google 上保留该页面的网页快照。