python爬虫框架Pyspider初次接触

  pyspider网站地址:http://docs.pyspider.org/en/latest/。文档比较好,安装起来也非常方便。既然是基于python的框架,那么首先得安装python。微软出的一款编辑软件VSCode,运行速度快,提供了丰富的插件,本人用它安装了python的插件,学习python。

       我分别在Linux、window上都安装过pyspider,window上貌似有问题。以下是我改写的一段代码:

      

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2018-04-18 07:17:21
# Project: emeraldinsight

from pyspider.libs.base_handler import *


class Handler(BaseHandler):
    crawl_config = {
    }

    @every(minutes=24 * 60)
    def on_start(self):
        self.crawl('https://www.emeraldinsight.com/action/doSearch?AllField=computer&content=articlesChapters', callback=self.index_page)

    @config(age=10 * 24 * 60 * 60)
    def index_page(self, response):

        for each in response.doc('.hlFld-Title a').items():
            self.crawl(each.attr.href, callback=self.list_page)

            
    @config(priority=2)
    def list_page(self, response):
        
        downloadurl='https://www.emeraldinsight.com/action/downloadCitation'
        
        detailUrl=response.url
        
        print '详细地址:'+detailUrl
        
        doi=detailUrl.replace('https://www.emeraldinsight.com/doi/full/','')

        print 'doi:'+doi
        
        postdata={
        
         'doi':doi,
         'format':'bibtex'
        }

        self.crawl(downloadurl,callback=self.detail_page,method='POST',data=postdata)

    @config(priority=2)
    def detail_page(self, response):
        print response.text

      这个简单的例子中包含有Get、Post请求以及对文档解析,它采用的是PQuery和JQuery的语法类似,所以上手特别快,几乎不用学习。

      此框架提供了任务调度、队列、文档解析、web端图形化的界面等。

       

posted @ 2018-05-03 15:12  micDavid  阅读(332)  评论(0编辑  收藏  举报