unexpected EOF while parsing Webscraping (BeautifulSoup)
I am trying to run this block code that is part of a WebScraping project of real estate data (https://github.com/arturlunardi/webscraping_vivareal/blob/main/scrap_vivareal.ipynb) but I am encountering errors in the card loop and cannot think of a solution (I am a beginner in the scraping part)
# Web-Scraping
for line in soup.findAll(class_="js-card-selector"):
try:
full_address=line.find(class_="property-card__address").text.strip()
address.append(full_address.replace('\n', '')) #Get all address
if full_address[:3]=='Rua' or full_address[:7]=='Avenida' or full_address[:8]=='Travessa' or full_address[:7]=='Alameda':
neighbor_first=full_address.strip().find('-')
neighbor_second=full_address.strip().find(',', neighbor_first)
if neighbor_second!=-1:
neighbor_text=full_address.strip()[neighbor_first+2:neighbor_second]
neighbor.append(neighbor_text)
else: # Bairro não encontrado
neighbor_text='-'
neighbor.append(neighbor_text)
else:
get_comma=full_address.find(',')
if get_comma!=-1:
neighbor_text=full_address[:get_comma]
neighbor.append(neighbor_text)
else:
get_hif=full_address.find('-')
neighbor_text=full_address[:get_hif]
neighbor.append(neighbor_text)
File "/tmp/ipykernel_70908/2775873616.py", line 26
^
SyntaxError: unexpected EOF while parsing
Does anyone have any idea what might be going on?
Solution 1:
You're not running the full block of code. Every try:
statement should be followed by an except:
. The code in the github link has it. The code you are showing that you are running does not. That's why you get the error.
for line in soup.findAll(class_="js-card-selector"):
try:
full_address=line.find(class_="property-card__address").text.strip()
address.append(full_address.replace('\n', '')) #Get all address
if full_address[:3]=='Rua' or full_address[:7]=='Avenida' or full_address[:8]=='Travessa' or full_address[:7]=='Alameda':
neighbor_first=full_address.strip().find('-')
neighbor_second=full_address.strip().find(',', neighbor_first)
if neighbor_second!=-1:
neighbor_text=full_address.strip()[neighbor_first+2:neighbor_second]
neighbor.append(neighbor_text)
else: # Bairro não encontrado
neighbor_text='-'
neighbor.append(neighbor_text)
else:
get_comma=full_address.find(',')
if get_comma!=-1:
neighbor_text=full_address[:get_comma]
neighbor.append(neighbor_text)
else:
get_hif=full_address.find('-')
neighbor_text=full_address[:get_hif]
neighbor.append(neighbor_text)
except:
continue