kotartemiy / pygooglenews

If Google News had a Python library
https://newscatcherapi.com
MIT License
1.26k stars 134 forks source link

Exception: Could not parse your date #27

Closed KevorkSulahian closed 2 years ago

KevorkSulahian commented 2 years ago

I have this very simple code

gn = GoogleNews()

start = datetime.date(2018,3,1)

end = datetime.date(2019,3,1)

print(start)

gn.search(query="car", from=start.strftime('%Y-%m-%d'), to=end.strftime('%Y-%m-%d'))

but it's giving me the error of `

AttributeError Traceback (most recent call last) /opt/anaconda3/lib/python3.8/site-packages/pygooglenews/init.py in __from_to_helper(self, validate) 89 try: ---> 90 validate = parse_date(validate).strftime('%Y-%m-%d') 91 return str(validate)

/opt/anaconda3/lib/python3.8/site-packages/dateparser/conf.py in wrapper(*args, *kwargs) 84 ---> 85 return f(args, **kwargs) 86 return wrapper

/opt/anaconda3/lib/python3.8/site-packages/dateparser/init.py in parse(date_string, date_formats, languages, locales, region, settings) 52 ---> 53 data = parser.get_date_data(date_string, date_formats) 54

/opt/anaconda3/lib/python3.8/site-packages/dateparser/date.py in get_date_data(self, date_string, date_formats) 416 for locale in self._get_applicable_locales(date_string): --> 417 parsed_date = _DateLocaleParser.parse( 418 locale, date_string, date_formats, settings=self._settings)

/opt/anaconda3/lib/python3.8/site-packages/dateparser/date.py in parse(cls, locale, date_string, date_formats, settings) 193 instance = cls(locale, date_string, date_formats, settings) --> 194 return instance._parse() 195

/opt/anaconda3/lib/python3.8/site-packages/dateparser/date.py in _parse(self) 197 for parser_name in self._settings.PARSERS: --> 198 date_obj = self._parsers[parser_name]() 199 if self._is_valid_date_obj(date_obj):

/opt/anaconda3/lib/python3.8/site-packages/dateparser/date.py in _try_parser(self) 221 self._settings.DATE_ORDER = self.locale.info.get('date_order', _order) --> 222 date_obj, period = date_parser.parse( 223 self._get_translated_date(), settings=self._settings)

/opt/anaconda3/lib/python3.8/site-packages/dateparser/conf.py in wrapper(*args, *kwargs) 84 ---> 85 return f(args, **kwargs) 86 return wrapper

/opt/anaconda3/lib/python3.8/site-packages/dateparser/date_parser.py in parse(self, date_string, settings) 36 stz = get_localzone() ---> 37 date_obj = stz.localize(date_obj) 38 else:

AttributeError: 'backports.zoneinfo.ZoneInfo' object has no attribute 'localize'

During handling of the above exception, another exception occurred:

Exception Traceback (most recent call last)

in 26 return stories 27 ---> 28 df = pd.DataFrame(get_news('Banana')) in get_news(search) 13 14 for date in date_list[:-1]: ---> 15 search = gn.search(search, from_=date.strftime('%Y-%m-%d'), to_=(date+delta).strftime('%Y-%m-%d')) 16 newsitem = search['entries'] 17 /opt/anaconda3/lib/python3.8/site-packages/pygooglenews/__init__.py in search(self, query, helper, when, from_, to_, proxies, scraping_bee) 139 140 if from_ and not when: --> 141 from_ = self.__from_to_helper(validate=from_) 142 query += ' after:' + from_ 143 /opt/anaconda3/lib/python3.8/site-packages/pygooglenews/__init__.py in __from_to_helper(self, validate) 91 return str(validate) 92 except: ---> 93 raise Exception('Could not parse your date') 94 95 Exception: Could not parse your date ` I would appreciate any help
KevorkSulahian commented 2 years ago

For those who get similar problem. parse_validate has a problem make sure to comment it out, it's a quick but not optimal solve

bottaluscio commented 2 years ago

I confirm the issue is still present

KevorkSulahian commented 2 years ago

@bottaluscio I copied the script and commented out parse_validate part :) hope it works for you too

bottaluscio commented 2 years ago

Thanks I was able to fix the method by re-implementing it without the date parsing check :)

ReemOmer commented 1 year ago

Thanks @KevorkSulahian, that works for me!

victorenriquenr commented 3 months ago

This was my solution,

At the beginning, in the file ../pygooglenews/init.py, replace the line : from dateparser import parse as parse_date

with

from datetime import datetime

and add:

def parse_date(date_string): return datetime.strptime(date_string, '%Y-%m-%d')