安装--pyspider

1.  官网下载

美 [ˈfæntəm]

2.找到下载并解压之后的文件  进行复制phantomjs.exe


3.  查看python路径, 将phantomjs.exe和python.exe放在同一目录下

4.  使用pyspider all命令  启动pyspider的所有组件

5.phantomjs也成功启动

6.  成功啦  哈哈哈


 7 .怎样删除创建的项目

8..自己学习去吧




# !/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2018-06-14 17:11:18
# Project: qiushi

from pyspider.libs.base_handler import *
import pymongo, requests
from fake_useragent import UserAgent
import time


def get_proxy():
    return requests.get('http://localhost:5010/get/').text


class Handler(BaseHandler):
    ua = UserAgent()
    headers = {
        'User-Agent': ua.random,
        # 'Host': 
        # 'Referer':
    }

    # 创建clientdb
    client = pymongo.MongoClient('localhost')
    db = client['qidian']

    # 对于整个爬虫项目的全局配置:所有的self.crawl()在请求的时候都会加载这个配置。
    crawl_config = {
        'headers': headers,
        # 'proxy': get_proxy(),
        'itag': 'v0'
    }

    # 增量爬虫1:每天重新启动爬虫的时候,只爬取页面上更新的数据。(采用去重策略)
    # 增量爬虫2url没有变化,数据更新了。(不能采用去重,每天都要重新爬取)
    # 项目启动首先进入的函数
    # @every: 用于设置定时爬取任务:可以是minutes, 也可以设置为seconds    @every(seconds=2 * 60)
    def on_start(self):
        # 初始爬取的url
        self.crawl('https://www.qidian.com/all', callback=self.index_page, validate_cert=False)

    # age: 主要是对任务url进行去重/过滤(根据taskid),每一个url有唯一的一个标识taskidage是一个以秒为单位的时间点,如果在这个时间范围内,遇到了相同的taskid,这个任务就会被丢弃。
    @config(age=60)
    def index_page(self, response):
        # response.doc返回一个pyquery对象
        for each in response.doc('h4 > a').items():
            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)

    # age的默认值是-1,永远不过期。
    @config(priority=2, age=60)
    def detail_page(self, response):
        # response中解析详情页的数据
        name = response.doc('h1 > em').text()
        author = response.doc('h1 a').text()
        tag = response.doc('.tag').text()
        info = response.doc('.intro').text()

        print(time.time())
        print(name)
        return {
            "time": str(time.time()),
            "url": response.url,
            "title": response.doc('title').text(),
            "name": name,
            "author": author,
            "tag": tag,
            "info": info,
        }

    # on_result是固定的函数,只要一个函数中有return,就会自动调用这个函数。
    def on_result(self, data):
        # detail_page函数返回的结果,保存至mongodb        # print('接收到数据了.....',response['url'])
        if data == None:
            print('')
        else:
            if self.db['q'].update_one({'name': data['name']}, {'$set': data}, True):
                print('数据保存成功')
            else:
                print('数据保存失败')


 


猜你喜欢

转载自blog.csdn.net/weixin_42312791/article/details/80695412