解析 Webscraping 时意外的 EOF (BeautifulSoup)
unexpected EOF while parsing Webscraping (BeautifulSoup)
我正在尝试 运行 这个块代码,它是房地产数据 WebScraping 项目的一部分 (https://github.com/arturlunardi/webscraping_vivareal/blob/main/scrap_vivareal.ipynb),但我在卡片循环中遇到错误,想不出解决方案(我是抓取部分的初学者)
# 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
有人知道会发生什么吗?
您不是运行 完整的代码块。每个 try:
语句后应跟一个 except:
。 github link 中的代码有它。您显示的代码表明您 运行 不是。这就是您收到错误的原因。
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
我正在尝试 运行 这个块代码,它是房地产数据 WebScraping 项目的一部分 (https://github.com/arturlunardi/webscraping_vivareal/blob/main/scrap_vivareal.ipynb),但我在卡片循环中遇到错误,想不出解决方案(我是抓取部分的初学者)
# 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
有人知道会发生什么吗?
您不是运行 完整的代码块。每个 try:
语句后应跟一个 except:
。 github link 中的代码有它。您显示的代码表明您 运行 不是。这就是您收到错误的原因。
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