什么是robots.txt文件? 搜索引擎使用spider程序自动访问互联网上的网页并获取网页信息。飞飞Asp技术乐~园spider在访问一个网站时,会首先会检查该网站的根域下是否有一个叫做robots.txt的纯文本文件,这个文件用于指定spider在您网站上的抓取范围。飞飞As@p技术乐园您可以在您的网站中创建一个robots.txt,在文件中声明该网站中不想被搜索引擎收录的部分或者指定搜索引擎只收录特定的部分。
请注意,仅当您的网站包含不希望被搜索引擎收录的内容时,才需要使用robots.txt文件。如果您希望搜索引擎收录网站上所有内容,请勿建立robots.txt文件。
———
robots.txt文件放在哪里?
robots.txt文件应该放置在网站根目录下。 举例来说,当spider访问一个网站(比如http://www.abc.com飞飞~Asp技术乐园)时,首先会检查该网站中是否存在http://www.abc.com/robots.txt这个文件,如果Spider找到这个文件,它就会根据这个文件的内容,来确定它访问权限的范围。 (robots.txt只能存放于网站的根目录下,每个网站,或每个域名(包括子域名),只能有一个robots.txt。)
———
如果您的网站在被百度收录之后才设置Robots文件禁止抓取,那么新的Robots文件通常会在48小时内生效,生效以后的新网页,将不再建入索引。需要注意的是,robots.txt禁止收录以前百度已收录的内容,从搜索结果中去除可能需要数月的时间。(百度官方说明...)
———
robots.txt 允许与禁止蜘蛛的写法
#禁止所有蜘蛛爬任何目录 User-agent: * Disallow: /
#允许所有蜘蛛爬任何目录 User-agent: * Disallow:
#允许百度蜘蛛爬,禁止其他蜘蛛爬任何目录 User-agent: baiduspider Disallow:
User-agent: * Disallow: /
———
robots.txt中的文件与目录
错误写法:
User-agent: * Disallow: /dir1/ /dir2/ /dir3/
正确写法:
User-agent: * Disallow: /dir1/ Disallow: /dir2/ Disallow: /dir3/ Disallow: /help.html
不允许所有搜索引擎蜘蛛爬dir1、dir2、dir3这三个目录及help.html这个文件! 特别注意的是,不要省略掉目录名后的“/”,不然,Spider便极有可能误读相应的设置。
———
robots.txt中限定项的顺序
错误写法:
User-agent: * Disallow: / User-agent: Googlebot Disallow:
该设定本意是想允许Google访问所有页面,同时禁止其他Spider的访问。但在这样的设置下,Googlebot在读取前2行后便会离开网站,后面对其的“解禁”完全失去了意义。
正确写法:
User-agent: Googlebot Disallow: User-agent: * Disallow: /
———
robots.txt 复杂较多的情况下该如何注释哪?
错误写法:
User-agent: Googlebot #这是对Google的设置 Disallow:
正确写法:
#这是对Google的设置 User-agent: Googlebot Disallow:
———
下表是权威,百度官方对外资料!
robots.txt文件用法举例
| 例1. 禁止所有搜索引擎访问网站的任何部分 |
User-agent: * Disallow: / |
|
例2. 允许所有的robot访问 (或者也可以建一个空文件 "/robots.txt") |
User-agent: * Disallow: 或者 User-agent: * Allow: / |
| 例3. 仅禁止Baiduspider访问您的网站 |
User-agent: Baiduspider Disallow: / |
| 例4. 仅允许Baiduspider访问您的网站 |
User-agent: Baiduspider Disallow:
User-agent: * Disallow: / |
例5. 禁止spider访问特定目录 在这个例子中,该网站有三个目录对搜索引擎的访问做了限制,即robot不会访问这三个目录。需要注意的是对每一个目录必须分开声明,而不能写成 "Disallow: /cgi-bin/ /tmp/"。 |
User-agent: * Disallow: /cgi-bin/ Disallow: /tmp/ Disallow: /~joe/ |
| 例6. 允许访问特定目录中的部分url |
User-agent: * Allow: /cgi-bin/see Allow: /tmp/hi Allow: /~joe/look Disallow: /cgi-bin/ Disallow: /tmp/ Disallow: /~joe/ |
例7. 使用"*"限制访问url 禁止访问/cgi-bin/目录下的所有以".htm"为后缀的URL(包含子目录)。 |
User-agent: * Disallow: /cgi-bin/*.htm |
例8. 使用"$"限制访问url 仅允许访问以".htm"为后缀的URL。 |
User-agent: * Allow: .htm$ Disallow: / |
| 例9. 禁止访问网站中所有的动态页面 |
User-agent: * Disallow: /*?* |
例10. 禁止Baiduspider抓取网站上所有图片 仅允许抓取网页,禁止抓取任何图片。 |
User-agent: Baiduspider Disallow: .jpg$ Disallow: .jpeg$ Disallow: .gif$ Disallow: .png$ Disallow: .bmp$ |
例11. 仅允许Baiduspider抓取网页和.gif格式图片 允许抓取网页和gif格式图片,不允许抓取其他格式图片 |
User-agent: Baiduspider Allow: .gif$ Disallow: .jpg$ Disallow: .jpeg$ Disallow: .png$ Disallow: .bmp$ |
| 例12. 仅禁止Baiduspider抓取.jpg格式图片 |
User-agent: Baiduspider Disallow: .jpg$ |
robots.txt文件参考资料
robots.txt文件的更具体设置,请参看以下链接:
· Web Server Administrator's Guide to the Robots Exclusion Protocol · HTML Author's Guide to the Robots Exclusion Protocol · The original 1994 protocol description, as currently deployed · The revised Internet-Draft specification, which is not yet completed or implemented
|