Python:对象没有metadata_parser 0.6.6的属性

标签 python object attributes metadata

我正在尝试 metadata_parser 0.6.6在Python中。 他们在他们的页面上给出了说明,我遵循这些说明:

    $ pip install metadata_parser
    $ python
    $ >>> import metadata_parser
    $ >>> page = metadata_parser.MetadataParser(url="http://www.cnn.com")
    $ >>> print page.metadata

这会打印元数据:

    {'og': {}, 'twitter': {}, 'meta': {'msapplication-TileImage': 'http://i.cdn.turner.com/cnn/2012/images/10/15/cnn_logo_144_144.png', 'viewport': 'width=1024', 'googlebot': 'noarchive', 'application-name': 'CNN', 'fb:app_id': '80401312489', 'msapplication-TileColor': '#CA0002', 'robots': 'index,follow', 'refresh': '1800;url=http://www.cnn.com/?refresh=1', 'msapplication-tooltip': 'Breaking News, U.S., World, Weather, Entertainment and Video News', 'last-modified': '2014-08-12T15:19:13Z', 'X-UA-Compatible': 'IE=edge', 'keywords': 'CNN,CNN news,CNN.com,CNN TV,news,news online,breaking news,U.S. news,world news,weather,business,CNN Money,sports,politics,law,technology,entertainment,education,travel,health,special reports,autos,developing story,news video,CNN Intl', 'fb:page_id': '129343697106537', 'content-type': u'text/html;charset=utf-8', 'msapplication-task': 'name=iReport;action-uri=http://ireport.cnn.com;icon-uri=http://ireport.cnn.com/favicon.ico', 'description': 'CNN.com delivers the latest breaking news and information on the latest top stories, weather, business, entertainment, politics, and more. For in-depth coverage, CNN.com provides special reports, video, audio, photo galleries, and interactive guides.'}, 'dc': {}, 'page': {'canonical': 'http://www.cnn.com/', 'title': u'CNN.com - Breaking News, U.S., World, Weather, Entertainment & Video News'}}

接下来我尝试按照他们的示例提取它:

    $ >>> print page.get_field('title')

但是,它给了我:“

    Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'MetadataParser' object has no attribute 'get_field'" 

其余示例代码行类似:

    $ >>> print page.get_field('title', strategy=['og',])
    $ >>> print page.get_field('title', strategy=['page', 'og', 'dc',])

我测试对象是否有属性:

    $ >>> hasattr(page, 'get_field')
    $ False

我能做什么呢?谢谢!

Python 版本,如果有帮助的话:

    $ >>> import sys
    $ >>> print (sys.version)
    $ 2.7.3 (default, Sep 26 2013, 20:03:06) 
    $ [GCC 4.6.3]

在使用 python 2.7.7 的不同机器上也有同样的情况。

最佳答案

metadata_parser 文档不正确。 MetadataParser 类没有定义 get_field 方法,但它确实定义了 get_metadata 方法,这似乎是你想要做什么:

class MetadataParser(object):
<--- *snip* --->
    def get_metadata(self, field, strategy=None):
    …

所以,你会使用:

$ >>> print page.get_metadata('title')
CNN.com - Breaking News, U.S., World, Weather, Entertainment & Video News

关于Python:对象没有metadata_parser 0.6.6的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25268696/

相关文章:

python - 如何仅在Python中根据属性在XML中查找(孙) child ?

python - 将 Autocomplete=off 应用到 Django 密码重置确认表单

javascript - 如果在对象中找到,则使用 jQuery 中的另一个属性

python - 如何在 __init__ 上创建 __len__ 函数?

python - 如何知道scrapy蜘蛛当前使用的是哪个用户代理?

.net - 与 SerializableAttribute 相比,使用 DataContractAttribute 有什么优势?

python - 使用 ReportLab 添加到 PDF 时 PNG 模糊

Python 和列表框 (GUI)

javascript - 在javascript对象选择器中使用变量

java - 如何判断 Selenium for Java 中是否选中了一个复选框?