Как сделать исключение?
Во время парсинга отелей, у одного из тегов 'a' нету класса(у остальных есть). Как мне сделать исключение?
код:
import requests
import lxml
from bs4 import BeautifulSoup
import csv
def get_data(url):
headers = {
'User Agent': 'Mozilla / 5.0(WindowsNT10.0; Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 110.0.0.0 Safari / 537.36'
}
req = requests.get(url=url)
with open('index.html', 'w', encoding='utf8') as file:
file.write(req.text)
soup = BeautifulSoup(req.text, 'lxml')
hotel_cards = soup.find_all('li', class_='item')
#print(hotel_cards)
for hotel_url in hotel_cards:
hotel_url = 'https://101hotels.com' + hotel_url.find('a').get('href')
#print(hotel_url)
for title_hotels in hotel_cards:
title_hotels = title_hotels.find('a', class_='has_query_params').text
#print(title_hotels)
def main():
get_data('https://101hotels.com/main/cities/yuzhno-sakhalinsk')
if __name__ == '__main__':
main()
Источник: Stack Overflow на русском