Attribute error: 'list' object has no attribute 'strftime' while using pygooglenews

231 views Asked by At

I am trying to get headlines and publishing time for news articles using pygooglenews. I want to be able to get daily articles over a period of 15 days, I have written the code below to specify start and end dates. but I get an error saying 'list object has no attribute strftime'

import datetime
from datetime import date
gn = GoogleNews(lang = 'en', country = 'US')
start_date = datetime.date(2021, 9, 1)
end_date = datetime.date(2021,9,14)
delta = datetime.timedelta(days=1)
search = gn.search('AAPL', from_=date.strftime('%Y-%m-%d'), to_=(date+delta).strftime('%Y-%m-%d'))
#search = gn.search('AAPL', when = '12m')
#search = gn.search('AAPL', from_=datetime.date.strftime('2016-09-14'), to_=datetime.date.strftime('2021-09-14'))

links=[]
for item in search['entries']:
  links.append(item.title)

date=[]
for item in search['entries']:
  date.append(item.published)
import pandas as pd
d={"Headline": links, "Timestamp": date}
df = pd.DataFrame(d)
df.to_csv('/content/drive/MyDrive/google_news_Apple_1y.csv')

the error

> Full error stacktrace: TypeError                                
> Traceback (most recent call last) <ipython-input-22-f1b0926565ea> in
> <module>()
>       3 end_date = datetime.date(2021,9,14)
>       4 delta = datetime.timedelta(days=1)
> ----> 5 search = gn.search('AAPL', from_=date.strftime('%Y-%m-%d'), to_=(date+delta).strftime('%Y-%m-%d'))
>       6 #search = gn.search('AAPL', when = '12m')
>       7 #search = gn.search('AAPL', from_=datetime.date.strftime('2016-09-14'),
> to_=datetime.date.strftime('2021-09-14'))
> 
> TypeError: descriptor 'strftime' requires a 'datetime.date' object but
> received a 'str

I understand I will probably have to loop through the 'list' search, but I am not able to figure out how. Please help me out with this, thanks!

1

There are 1 answers

1
buran On BEST ANSWER

I think you mean

search = gn.search('AAPL', from_=start_date.strftime('%Y-%m-%d'), to_=(start_date+delta).strftime('%Y-%m-%d'))

or maybe using end_date, but in any case not date.