Reputation: 13
The code I'm using has been working occasionally, but I need something to work all the time. The site I'm scraping sometimes has the info and other times omits it, which is why I get the error after running the program. Since the site doesn't always post a time for certain events, 'event_time' obviously is not defined.
The problem I'm facing is that sometimes the with suppress(Exception):
works and sometimes it doesn't. What can I add to skip over the the event_time when the site doesn't specify a time, and have the program continue to scrape the rest of the site?
event_name = s.select_one('.eventname').get_text(strip=True)
event_day = s.select_one('.date').text.strip()
event_month = s.select_one('.month').text.strip()
with suppress(Exception):
event_time = s.select_one('.time').text.strip()
event_info = s.select_one('.event-header').text.strip()
Error:
NameError
Traceback (most recent call last)
<ipython-input-49-45cf21eb3177> in <module>
22 print('Dia: ' + event_day)
23 print('Mes: ' + event_month)
---> 24 print('Hora: ' + event_time)
25 print('Descripción: ' + event_info)
26 print('-' * 80)
NameError: name 'event_time' is not defined
Upvotes: 0
Views: 838
Reputation: 229
You can use a try-except statement to ensure that the exception is caught every time.
try:
event_time = s.select_con('.time').text.strip()
except NameError:
event_time = ...
Upvotes: 0
Reputation: 77337
Instead of supressing the exception
with suppress(Exception):
event_time = s.select_one('.time').text.strip()
catch it and add a default
try:
event_time = s.select_con('.time').text.strip()
except Exception:
event_time = ''
Catching a generic exception can hide other errors. Its best to figure out which exceptions can happen and catch those.
Upvotes: 1