I am using the script below to get the Google News results for a brand. However, I would love to be able to export the results to a CSV or Excel file. Can someone help me out, please?
pip install pygooglenews --upgrade
from pygooglenews import GoogleNews
gn = GoogleNews(country = 'IE')
def get_titles(search):
stories = []
search = gn.search(search)
newsitem = search['entries']
for item in newsitem:
story = {
'title' : item.title,
'link' : item.link
}
stories.append(story)
return stories
print(get_titles('aldi'))
CodePudding user response:
An easy (but heavy) way would be to use pandas, which easily converts a list of dictionaries into a data frame
import pandas as pd
pd.DataFrame(stories).to_csv("result_list.csv")
The built-in way is answered here: How do I convert this list of dictionaries to a csv file?
import csv
with open("results_list.csv", "w", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=stories[0].keys())
writer.writeheader()
writer.writerows(stories)
CodePudding user response:
You could use the CSV module to write the rows as you pull them from the search.
from pygooglenews import GoogleNews
import csv
gn = GoogleNews(country = 'IE')
def write_csv(filename, search):
search = gn.search(search)
with open(filename, "w", newline="") as fileobj:
csv.writer(fileobj).writerows((item.title, item.link)
for item in search['entries'])
write_csv('output.csv', 'aldi')
CodePudding user response:
You can try the below.
Get the data as list of dicts, loop over the list and write each entry to the file.
from pygooglenews import GoogleNews
gn = GoogleNews(country = 'IE')
def get_titles(search):
stories = []
search = gn.search(search)
newsitem = search['entries']
for item in newsitem:
story = {
'title' : item.title,
'link' : item.link
}
stories.append(story)
return stories
stories = get_titles('aldi')
with open('stories.csv','w',encoding='utf-8') as f:
f.write('title,link\n')
for s in stories:
f.write(f'{s["title"]},{s["link"]}\n')
stories.csv
title,link
Aldi to trial first checkout-free supermarket - BreakingNews.ie,https://www.breakingnews.ie/business/aldi-to-trial-first-checkout-free-supermarket-1186611.html
Green light given to plans for new Aldi store in Ennis - Clare Champion,https://clarechampion.ie/green-light-given-to-plans-for-new-aldi-store-in-ennis/
Kildare woman scoops up top prize at Aldi's National Brown Bread Baking Competition 2021 - Leinster Leader,https://www.leinsterleader.ie/news/local-news/666996/kildare-woman-scoops-up-top-prize-at-aldi-s-national-brown-bread-baking-competition-2021.html
Aldi to open new shops in Co Mayo and Co Galway - RTE.ie,https://www.rte.ie/news/business/2021/0909/1245582-two-new-aldi-stores-for-connacht/
Aldi, Lidl and Tesco Ireland customers reveal their favourite 'hidden gems' with some unknown bargains - Irish Mirror,https://www.irishmirror.ie/lifestyle/aldi-lidl-tesco-ireland-customers-25021723
"It's a massive boost for us." Grow with Aldi winners - Newstalk,https://www.newstalk.com/podcasts/highlights-from-the-pat-kenny-show/its-a-massive-boost-for-us-grow-with-aldi-winners
Gourmet sausage, burger sauce, cookies: Winners of Grow with Aldi announced - Agriland,https://www.agriland.ie/farming-news/gourmet-sausage-burger-sauce-cookies-winners-of-grow-with-aldi-announced/
Elderly Clare couple fined for refusing to wear masks in Aldi - Clare Champion,https://clarechampion.ie/elderly-clare-couple-fined-for-refusing-to-wear-masks-in-aldi/
Tesco, Aldi, M&S and Morrisons supermarkets acquired by real estate investment giant in £113.1m deal - Business Live,https://www.business-live.co.uk/commercial-property/tesco-aldi-ms-morrisons-supermarkets-21616148
Aldi And IRFU Launch New Cookbook In Support Of Barnardos - Irish Rugby,https://www.irishrugby.ie/2021/09/12/aldi-and-irfu-launch-new-cookbook-in-aid-of-barnardos/
Aldi unveils plan to build store in Thames Ditton with 50 jobs created - Surrey Live,https://www.getsurrey.co.uk/news/surrey-news/aldi-unveils-plan-build-store-21625481
Aldi Salad Dressing Is Getting Recalled Due To Contamination - Delish.com,https://www.delish.com/food-news/a37665205/aldi-salad-dressing-recall-september-2021/
...