Reputation: 37
I'm writing a basic webscraping code, which basically will get the date and title of a post and, if the date >= a date stored in a SQL db and the title != from a title also stored in the DB, then do some stuff.
I'm getting a syntax error when executing it. Here's the code:
for i in web_results:
py_newsdate = datetime.strptime(i.find('div', attrs={'class': "infoItem"}).find_all('p')[0].getText()[6:], '%d/%m/%Y')
py_newstitle = i.find('h3').find('a')['title']
if (py_newsdate < sql_latest[0][0] or py_newstitle == sql_latest[0][1]):
else:
py_newslink = i.find('h3').find('a')['href']
web_results_final.append([py_newsdate, py_newstitle, py_newslink])
print(web_results_final)
And here's the error:
File "searcher", line 37
else:
^
IndentationError: expected an indented block
I know the blocks have to be properly idented, but I'm thinking the problem may be on this "empty" if.
Thanks.
Upvotes: 0
Views: 224
Reputation: 51
It will be more efficient if you use,
if not (py_newsdate < sql_latest[0][0] or py_newstitle == sql_latest[0][1]):
py_newslink = i.find('h3').find('a')['href']
web_results_final.append([py_newsdate, py_newstitle, py_newslink])
Upvotes: 1
Reputation: 81988
Blocks need to be followed by something. If you have a block you intend to leave empty, then use the pass
statement:
if (py_newsdate < sql_latest[0][0] or py_newstitle == sql_latest[0][1]):
pass
else:
py_newslink = i.find('h3').find('a')['href']
web_results_final.append([py_newsdate, py_newstitle, py_newslink])
This is true whether you are trying to write the block inline or on multiple lines:
if 0: pass
else: print(2)
Note: you have to have a new line for an else
block. This will not work:
if 0: pass; else: print(2)
Upvotes: 2