zoukankan      html  css  js  c++  java
  • pyspider 示例

    数据存放目录:

    C:UsersAdministratordata

    升级版(可加载文章内所有多层嵌套的图片标签)

    #!/usr/bin/env python
    # -*- encoding: utf-8 -*-
    # Created on 2019-04-08 14:24:34
    # Project: qunaer
    
    from pyspider.libs.base_handler import *
    
    
    class Handler(BaseHandler):
        crawl_config = {
        }
    
        @every(minutes=24 * 60)
        def on_start(self):
            self.crawl('https://travel.qunar.com/travelbook/list.htm', callback=self.index_page,validate_cert=False)
    
        @config(age=10 * 24 * 60 * 60)
        def index_page(self, response):
            for each in response.doc('li > .tit>a').items():
                self.crawl(each.attr.href, callback=self.detail_page,validate_cert=False,fetch_type='js',js_viewport_height='100000')
            next1=response.doc('.next').attr.href
            self.crawl(next1,callback=self.index_page,validate_cert=False)
    
        @config(priority=2)
        def detail_page(self, response):
            imgs=response.doc('.js_memo_node').find('img')#获取id下的所有(包括多层嵌套的)img标签
            img_list=''                                  #必须事先声明,否则return,img_list时会报错:引用未事先声明的局部变量
            for img in imgs.items():
                img_list+=img.attr.src+','              #把所有图片用,组合在一起
                                                        #【复习】img_list=', '.join(['cats', 'rats', 'bats'])
            return {
                "url": response.url,
                "title": response.doc('title').text(),
                "date":response.doc('li.f_item.when > p > span.data').text(),
                "day":response.doc('li.f_item.howlong > p > span.data').text(),
                "text":response.doc('#b_panel_schedule').text(),
                "img":img_list
                
            }
    #ele-3076663-2 > div.bottom > div.e_img_schedule > div > dl:nth-child(2) > dt > img

    例子A

    #!/usr/bin/env python
    # -*- encoding: utf-8 -*-
    # Created on 2019-04-08 14:24:34
    # Project: qunaer
    
    from pyspider.libs.base_handler import *
    
    
    class Handler(BaseHandler):
        crawl_config = {
        }
    
        @every(minutes=24 * 60)
        def on_start(self):
            self.crawl('https://travel.qunar.com/travelbook/list.htm', callback=self.index_page,validate_cert=False)
    
        @config(age=10 * 24 * 60 * 60)
        def index_page(self, response):
            for each in response.doc('li > .tit>a').items():
                self.crawl(each.attr.href, callback=self.detail_page,validate_cert=False,fetch_type='js',js_viewport_height='100000')#用js加载,指定页面高度,防止懒加载图片只加载一半
            next1=response.doc('.next').attr.href
            self.crawl(next1,callback=self.index_page,validate_cert=False)
    
        @config(priority=2)
        def detail_page(self, response):
            #imgs=response.doc('#js_mainleft').find('img')
            #for img in imgs.items():
            #    img_list=img_list+img+','
            
            return {
                "url": response.url,
                "title": response.doc('title').text(),
                "date":response.doc('li.f_item.when > p > span.data').text(),
                "day":response.doc('li.f_item.howlong > p > span.data').text(),
                "text":response.doc('#b_panel_schedule').text(),
                "img":response.doc('#js_mainleft').find('img').attr.src
                
            }
    #ele-3076663-2 > div.bottom > div.e_img_schedule > div > dl:nth-child(2) > dt > img
  • 相关阅读:
    天天共写了四本书,如果您使用有问题欢迎反馈
    难道QQ没有办法封?
    一个包含ASP.NET2.0全部登陆控件在典型身份验证的考虑
    数据访问模式二:数据集和数据适配器(传统的数据访问模式)
    数据访问两种模式的比较
    在母版页里使用FindControl的困惑
    有了中文版MSDN2,何必又东奔西走去找资料
    ASP.NET2.0技术详解与应用实例
    ASP.NET2.0新功能之客户端脚本功能
    使用ASP.NET2.0的TreeView控件显示分层数据
  • 原文地址:https://www.cnblogs.com/chenxi188/p/10689335.html
Copyright © 2011-2022 走看看