Python - Exporting PyGoogleNews output to CSV

442 views Asked by At

I am new to Python and am trying to extract scraped headlines from the Google News Feed using the PyGoogleNews library for a project. I am using Google Colab. The PyGoogleNews code is running perfectly for me and I am happy with that. The code is creating the csv but I have been unable to populate the csv with the scraped headline results. I want to export the scraped headlines output into a csv file as I will be performing a sentiment analysis and downloading/extracting it to perform further analysis on it. I would be really grateful for any help as this has been bugging me for days, I am sure it is something very obvious! Thank you in advance.

!pip install pygooglenews --upgrade 
import pandas as pd
import csv
from pygooglenews import GoogleNews

gn = GoogleNews (lang = 'en', country = 'UK') 

Xmassearch = gn.search('intitle:Christmas', helper = True, from_ = '2019-12-01', to_= '2019-12-31')

print(Xmassearch['feed'].title)

for item in Xmassearch ['entries']:
  print(item['title'])

file = open("Christmassearch.csv", "w")
writer = csv.writer(file)

writer.writerow(["Xmassearch"])

file.close()
1

There are 1 answers

0
Zach Young On

I don't have GoogleNews, and I cannot find any documentation that uses the keyword entries, so I cannot say exactly how this should work, so...

If iterating Xmassearch['entries'] works for you, then move that iteration down to where you've opened the CSV and have started writing.

I also re-wrote/structured your file handling to use Python's with compound statement so you don't have to manage the closing of the file:

with open('Christmassearch.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(['Xmassearch'])  # I presume you meant this to be your header
    
    # Use your loop from before...
    for item in Xmassearch ['entries']:
        # And write each item
        writer.writerow([item['title']])