本文介绍了在python中读取protobuf.提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理来自 spinn3r 的数据.数据作为 protobuf 返回.在python中,当我打印protobuf对象时,我得到了这个:

I am trying to work with data from spinn3r. The data is returned as a protobuf. In python, when I print the protobuf object, I get this:

print data
source {
  link {
    href: ""
    resource: ""
  }
  canonical_link {
    href: "http://twitter.com/_PattiShaw/statuses/28167079857225728"
    resource: ""
  }
  title: ""
  hashcode: ""
  lang {
    code: "en"
    probability: -1.0
  }
  generator: ""
  description: ""
  last_posted: ""
  last_published: ""
  date_found: ""
  publisher_type: "MICROBLOG"
}
feed {
  link {
    href: ""
    resource: ""
  }
  canonical_link {
    href: ""
    resource: ""
  }
  title: ""
  hashcode: ""
  lang {
    code: "en"
    probability: -1.0
  }
  generator: ""
  description: ""
  last_posted: ""
  last_published: ""
  date_found: ""
  etag: ""
  channel_link {
    href: ""
    resource: ""
  }
}
feed_entry {
  link {
    href: "http://twitter.com/_PattiShaw/statuses/28167079857225728"
    resource: "http://twitter.com/_PattiShaw/statuses/28167079857225728"
  }
  canonical_link {
    href: "http://twitter.com/_PattiShaw/statuses/28167079857225728"
    resource: "http://twitter.com/_PattiShaw/statuses/28167079857225728"
  }
  title: "The value of a man resides in what he gives and not in what he is capable of receiving. ~ Albert Einstein"
  hashcode: "8WhKLK9Lyng"
  lang {
    code: "en"
    probability: -1.0
  }
  author {
    name: "_PattiShaw (Patti Shaw)"
    email: ""
    link {
      href: "http://twitter.com/_PattiShaw"
    }
  }
  spam_probability: 0.0
  last_published: "2011-01-20T19:08:49Z"
  date_found: "2011-01-20T19:08:49Z"
  identifier: 1295550574016007548
  content {
    mime_type: "text/html"
    data: "x\332M\214\301\r\2000\014\304V\271\t`\201\n\211\007\033\260@B\003\215TR\324\226\362cv\020/\276\266\3459\010\032\305S\220V\020v2d)\352\245@\rW\240\212\267\330\264\275\300\361@\346]\317\003,\325\277\327\202\205\016\342\370m\262,\242Mm\353pc\214,\271bR+U\324\036\200\236&\363"
    encoding: "zlib"
  }
}
permalink_entry {
  link {
    href: "http://twitter.com/_PattiShaw/statuses/28167079857225728"
    resource: "http://twitter.com/_PattiShaw/statuses/28167079857225728"
  }
  canonical_link {
    href: "http://twitter.com/_PattiShaw/statuses/28167079857225728"
    resource: "http://twitter.com/_PattiShaw/statuses/28167079857225728"
  }
  title: "The value of a man resides in what he gives and not in what he is capable of receiving. ~ Albert Einstein"
  hashcode: "8WhKLK9Lyng"
  lang {
    code: "en"
    probability: -1.0
  }
  author {
    name: "_PattiShaw (Patti Shaw)"
    email: ""
    link {
      href: "http://twitter.com/_PattiShaw"
    }
  }
  spam_probability: 0.0
  last_published: "2011-01-20T19:08:49Z"
  date_found: "2011-01-20T19:09:34Z"
  identifier: 1295550574016007548
  content {
    mime_type: "text/html"
    data: ""
  }
  content_extract {
    mime_type: "text/html"
    data: ""
  }
  generator: ""
}

我想从feed_entry"对象中提取作者姓名".我试过这个:

I want to extract the "author name" from the "feed_entry" object. I tried this:

print data.feed_entry.author.name

我收到错误:

AttributeError: 'RepeatedCompositeFieldContainer' object has no attribute 'name'

我试着只打印作者对象来看看会发生什么.这是我得到的:

I tried just printing the author object to see what happens. This is what I got:

print u.feed_entry.author
[<spinn3rApi_pb2.Author object at 0x362e6d0>]

如何提取作者姓名?

推荐答案

看起来 u.feed_entry.author 是一个列表.注意方括号:

It looks like u.feed_entry.author is a list. Note the square brackets:

[<spinn3rApi_pb2.Author object at 0x362e6d0>]

这应该可以解决您的问题(假设您至少有一位作者):

This should solve your problem (assuming you have at least one author):

print data.feed_entry.author[0].name

这篇关于在python中读取protobuf.提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 04:36