pythonweb-scrapingpython-newspapernewspaper3k

Why is.summary on the Python newspaper3k module returning blank?


I'm presently coding a quick python script to summarize a given news article using the newspaper3k module

The following code to retrieve and print the text in the terminal works fine.

import newspaper

# Assign url
url = 'url'

# Extract web data
url_i = newspaper.Article(url="%s" % (url), language='en')
url_i.download()
url_i.parse()


# Display scraped data
print(url_i.text)

However when i replace the ".text" method in the last line with the ".summary" Nothing pops up, though i still get a code zero indicating that the compiler found no errors

It seems that it is working but is just not displaying for some reason.

Thanks.

Tried looking at the documentation and online but .summary seems to work just fine for everyone else.


Solution

  • Newspaper3k has a special syntax to print an article summary.

    Here is an example from my Newspaper3kusage documentation

    from newspaper import Config
    from newspaper import Article
    
    USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'
    
    config = Config()
    config.browser_user_agent = USER_AGENT
    config.request_timeout = 10
    
    base_url = 'https://www.theguardian.com/news/2020/dec/08/the-curse-of-white-oil-electric-vehicles-dirty-secret-lithium'
    article = Article(base_url, config=config)
    article.download()
    article.parse()
    article.nlp()
    print(article.summary)
    

    Output:

    The sudden excitement surrounding petróleo branco (“white oil”) derives from an invention rarely seen in these parts: the electric car.
    More than half (55%) of global lithium production last year originated in just one country: Australia.
    The Portuguese government is preparing to offer licences for lithium mining to international companies in a bid to exploit its “white oil” reserves.
    As manufacture has slowed down, a glut of lithium on global markets has dampened the white oil boom, if only temporarily.
    If people were better informed, he reasoned, it’s just possible that public opinion could swing to their side, and the country’s lithium mining plans could get shelved.