diff --git a/ReadMe.md b/ReadMe.md index 2d898a2e..abb10cb7 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -256,3 +256,46 @@ Scrapy是一个基于Twisted的开源的Python爬虫框架,在工业中应用 相关内容可以参考[基于Scrapy网络爬虫的搭建](http://www.lining0806.com/%E5%9F%BA%E4%BA%8Escrapy%E7%BD%91%E7%BB%9C%E7%88%AC%E8%99%AB%E7%9A%84%E6%90%AD%E5%BB%BA/),同时给出这篇文章介绍的[微信搜索](http://weixin.sogou.com/weixin)爬取的项目代码,给大家作为学习参考。 参考项目:[使用Scrapy或Requests递归抓取微信搜索结果](https://github.com/lining0806/PythonSpiderNotes/blob/master/WechatSearchProjects) + +## Robots协议 + +好的网络爬虫,首先需要遵守**Robots协议**。Robots协议(也称为爬虫协议、机器人协议等)的全称是“网络爬虫排除标准”(Robots Exclusion Protocol),网站通过Robots协议告诉搜索引擎哪些页面可以抓取,哪些页面不能抓取。 + +在网站根目录下放一个robots.txt文本文件(如 https://www.taobao.com/robots.txt ),里面可以指定不同的网络爬虫能访问的页面和禁止访问的页面,指定的页面由正则表达式表示。网络爬虫在采集这个网站之前,首先获取到这个robots.txt文本文件,然后解析到其中的规则,然后根据规则来采集网站的数据。 + +### 1. Robots协议规则 + + User-agent: 指定对哪些爬虫生效 + Disallow: 指定不允许访问的网址 + Allow: 指定允许访问的网址 + +注意: 一个英文要大写,冒号是英文状态下,冒号后面有一个空格,"/"代表整个网站 + +### 2. Robots协议举例 + + 禁止所有机器人访问 + User-agent: * + Disallow: / + 允许所有机器人访问 + User-agent: * + Disallow: + 禁止特定机器人访问 + User-agent: BadBot + Disallow: / + 允许特定机器人访问 + User-agent: GoodBot + Disallow: + 禁止访问特定目录 + User-agent: * + Disallow: /images/ + 仅允许访问特定目录 + User-agent: * + Allow: /images/ + Disallow: / + 禁止访问特定文件 + User-agent: * + Disallow: /*.html$ + 仅允许访问特定文件 + User-agent: * + Allow: /*.html$ + Disallow: / \ No newline at end of file diff --git a/WechatSearchProjects/Wechatproject/Wechatproject/pipelines.py b/WechatSearchProjects/Wechatproject/Wechatproject/pipelines.py index 7bf76f23..1c93d38a 100644 --- a/WechatSearchProjects/Wechatproject/Wechatproject/pipelines.py +++ b/WechatSearchProjects/Wechatproject/Wechatproject/pipelines.py @@ -18,7 +18,7 @@ # host = "localhost", # db = "testwechat", # you must build database named testwechat # user = "root", -# passwd = "fireling", +# passwd = "testpasswd", # charset = "utf8") # # pipeline default function # def process_item(self, item, spider): @@ -37,7 +37,7 @@ class WechatprojectPipeline(object): def __init__(self): connection = pymongo.Connection(host = "localhost", port = 27017) db = connection["testwechat"] # you need no build database named testdouban - # db.authenticate(name = "root", password = "fireling") # no name and password for localhost + # db.authenticate(name = "root", password = "testpasswd") # no name and password for localhost self.posts = db["result"] # you need not build collection named book # pipeline default function def process_item(self, item, spider):