robots.txt 和 sitemap
2008 年 12 月 22 日
为了爬虫, 在网站根目录创建robots.txt文件(utf-8),文件内容涉及四类键值对:User-agent,Disallow,Allow,Sitemap,支持正则, Disallow后没内容就是允许所有
User-agent: * Disallow: Disallow: /cgi-bin/ Disallow: /admin/ Sitemap: https://zdb.im/sitemap.xml
sitemap四种写法:
第一种, 文本格式, sitemap.txt, 一行一个URL地址
https://zdb.im/ https://zdb.improject.html
第二种, HTML格式, 允许加style标签, 一般布局是 html>body>ul>li>a[href,title]>plainText, URL用全路径, 爬虫递归往下找;
第三种, 单个XML文件, 默认文件名sitemap.xml, 百度是sitemap_baidu.xml
权重: 0.0到1.0,
设备: 代码适配,自适应设备,移动端,默认不填是PC端;
刷新频率: always 经常, hourly 每小时,daily 每天,weekly 每周, monthly 每月, yearly 每年, never 从不
修改时间: 2019-11-28或2019-11-28T11:59:38+00:00 皆可, 建议第二种;
年:YYYY(2005)
年和月:YYYY-MM(2005-06)
年月日:YYYY-MM-DD(2005-06-04)
年月日小时分钟:YYYY-MM-DDThh:mmTZD(2005-06-04T10:37+08:00)
年月日小时分钟秒:YYYY-MM-DDThh:mmTZD(2005-06-04T10:37:30+08:00)
https://zdb.im/ 0.5 2019-11-28T11:59:38+00:00 hourly https://zdb.im/project.html 0.5 2019-11-28 hourly
第四种, 当第三种的url个数超过5万个或未压缩时大小超过10MB(10,485,760字节), 建立索引文件(sitemap index),指向不超过1,000个Sitemap的URL; 索引文件的默认文件名 sitemap_index.xml;
路径必填 loc; 修改时间选填 lastmod 2009-12-14;
https://zdb.im/sitemap.xml
百度支持自动推送: 页面JS推送,RPC通知,URL POST
(function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https'){ bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else{ bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();
百度资源搜索平台,添加站点,得到token,用HTTP主动推送;
https://ziyuan.baidu.com/linksubmit/index?site=,
1)curl推送示例 将要提交的url数据写入一个本地文件,比如urls.txt,每个url占一行,然后调用curl命令: curl -H 'Content-Type:text/plain' --data-binary @urls.txt "http://data.zz.baidu.com/urls?site=www.example.com&token=edk7yc4rEZP9pDQD" 使用php、python、java等可以参照这个过程推送结构化数据。 2)post推送示例 POST /urls?site=www.58.com&token=edk7ychrEZP9pDQD HTTP/1.1 User-Agent: curl/7.12.1 Host: data.zz.baidu.com Content-Length: 83 http://www.example.com/1.html http://www.example.com/2.html 3)php推送示例 $urls = array( 'http://www.example.com/1.html', 'http://www.example.com/2.html', ); $api = 'http://data.zz.baidu.com/urls site=www.58.com&token=edk7ychrEZP9pDQD'; $ch = curl_init(); $options = array( CURLOPT_URL => $api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => implode("\n", $urls), CURLOPT_HTTPHEADER => array('Content-Type: text/plain'), ); curl_setopt_array($ch, $options); $result = curl_exec($ch); echo $result;