(注意:实例为爬取起点中文网 >地址:https://www.qidian.com/all) 创建项目:
创建后项目内容:
实例 操作步骤:
数据库内容如下: 使用mongodb数据库:
代码如下:
# -*- encoding: utf-8 -*- # Created on 2018-06-14 17:11:18 from pyspider.libs.base_handler import * from fake_useragent import UserAgent return requests.get('http://localhost:5010/get/').text #由于代理池获取的免费代理,不稳定!因此后面的就不在使用代理,仅仅在这里把获取过程的封装函数放在这里!! class Handler(BaseHandler): client = pymongo.MongoClient('localhost') # 对于整个爬虫项目的全局配置:所有的self.crawl()在请求的时候都会加载这个配置。 # 增量爬虫1:每天重新启动爬虫的时候,只爬取页面上更新的数据。(采用去重策略) # 增量爬虫2:url没有变化,数据更新了。(不能采用去重,每天都要重新爬取) # @every: 用于设置定时爬取任务:可以是minutes, 也可以设置为seconds。 self.crawl('https://www.qidian.com/all', callback=self.index_page,validate_cert=False) # age: 主要是对任务url进行去重/过滤(根据taskid),每一个url有唯一的一个标识taskid,age是一个以秒为单位的时间点, #如果在这个时间范围内,遇到了相同的taskid,这个任务就会被丢弃。 def index_page(self, response): # response.doc返回一个pyquery对象 for each in response.doc('h4 > a').items(): #fetch_type='js', js_script="":如果是javascript代码,需要在self.crawl()加入该参数 self.crawl(each.attr.href, callback=self.detail_page, validate_cert=False) # next_page = response.doc('.lbf-pagination-item-list > li:nth-of-type(9) > a') # self.crawl(next_page.attr.href, callback=self.index_page, validate_cert=False) @config(priority=2,age=60) def detail_page(self, response): name = response.doc('h1 > em').text() author = response.doc('h1 a').text() tag = response.doc('.tag').text() info = response.doc('.intro').text() "time": str(time.time()), "title": response.doc('title').text(), # on_result是固定的函数,只要一个函数中有return,就会自动调用这个函数。 def on_result(self, data): # 将detail_page函数返回的结果,保存至mongodb中 #print('接收到数据了.....',response['url']) if self.db['q'].update_one({'name': data['name']}, {'$set': data}, True):
推荐一篇文章pyspider简易教程:网址>https://www.jianshu.com/p/36290e6acf45
|