Search code examples
pythonpython-newspaper

Everything ignored but the first link when re-entering the code by gathering text from newspapers


I need to gather the text of the articles from multiple URLs. Code functions perfectly when entered. However, by re-entering print(first_article.text) for exporting the output to CSV only the first article appears. Is there a reason why this is happening and how would it be possible to export the text from all files?

import newspaper
from newspaper import Article

lista = ['url','url']

for list in lista:
   first_article = Article(url="%s" % list, language='en')
   first_article.download()
   first_article.parse()
   print(first_article.text)
#This prints all articles

print(first_article)
#This prints only one

Reference: Downloading articles from multiple urls with newspaper


Solution

  • I think I see the problem. You want to get a list of articles. You can achieve this by appending a list:

     lista = ['url','url']
     articles = [] #initialize a list
     for list in lista:
        first_article = Article(url="%s" % list, language='en')
        first_article.download()
        first_article.parse()
        articles += [first_article.text] # Add article to list
        print(first_article.text)
    
     print(articles) #Print all articles