mel
mel

Reputation: 1606

Python: object has no attribute for metadata_parser 0.6.6

I am experimenting with metadata_parser 0.6.6 in Python. They give instructions on their page, which I follow:

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

This prints 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'}}

Next I attempt to extract it following their example:

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

However, it gives me: "

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

Similar for the rest of the example code lines:

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

I test if the object has an attribute:

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

What can I do about it? Thanks!

Python version, if that helps:

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

Same thing on a different machine with python 2.7.7.

Upvotes: 1

Views: 596

Answers (1)

Kellen
Kellen

Reputation: 611

The metadata_parser documentation is incorrect. The MetadataParser class does not define a get_field method, but it does define a get_metadata method, which seems to do what you're trying to do:

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

So, you'd use:

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

Upvotes: 1

Related Questions