错误AttributeError:'NoneType'对象没有属性'title' - >由于空课

时间:2016-07-20 14:37:28

标签: python xpath

我循环遍历xpath,获取所有黑色类的粗体文本。

然后在粗体类中获得标题。这个工作正常,直到我遇到一个内部空白。

使用下面的代码我如何编辑它来删除/跳过不包含数据的类?

var agenda = new agenda({db: {address: configParams.db}});

schedule_notifications = function(req) {

// define an agenda task named notify
    agenda.define('notify', function(job, done) {

     // create a gcm message
        var message = new gcm.Message({
            notification: { "body": 'test' }
        });
        var sender = new gcm.Sender('server id');
        var regTokens = ['phone id'];

        // send the message
        sender.send(message, { registrationTokens: regTokens }, function(err, response) {
            if (err) console.error(err);
            else console.log(response);
            done();
        });
    });


    // get the object from the request
    var req_json = JSON.parse(req.body.data),
        keys = Object.keys(req_json),
        key_string = keys[0],
        start_obj = new Date(req_json[key_string][0].start);

    // schedule the job with the date object found in the request
    // start_obj, for example could be made using
    // start_obj = new Date();
    // notify is the name of the job to run

    agenda.schedule(start_obj, 'notify');
    agenda.start();

// can comment agenda.schedule and uncomment the following line to delete the unfinished jobs in db
// agenda.purge(function(err, numRemoved) {});    
}

我得到的错误是:

  

AttributeError:'NoneType'对象没有属性'title'

由于这一行:

out = [b.text.title() + "##" + b.xpath("./following::text()[1]")[0].lstrip(",") for b in div.xpath(".//b[@class='black']")]

如果它有助于div.xpath:

<b class="black"></b>

1 个答案:

答案 0 :(得分:0)

只需在列表推导中添加一个过滤器:

out = [b.text.title() + "##" +  b.xpath("./following::text()[1]")[0].lstrip(",")
       for b in div.xpath(".//b[@class='black']")
       if b.text]

if b.text确保存在真实(非空Noneb.text属性值。