Ошибка в коде парсера питон, в чем ошибка?

from bs4 import BeautifulSoup
import requests

def parse():
	URL = 'https://www.olx.ua/list/'
	HEADERS = {
	 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
	}

	response = requests.get(URL,
	headers = HEADERS)
	soup = BeautifulSoup(response.content, 'html.parser')
	items = soup.findAll('div', class_ = 'offer-wrapper')
	comps = []

	for item in items:
		comps.append({
		  'title': item.find('a', class_ = 'marginright5 link linkWithHash detailsLink').get_text(strip = True),
		  'price': item.find('p', class_ = 'price').get_text(strip = True),
		  'link': item.find('a', class_ = 'marginright5 link linkWithHash detailsLink').get('href')
		})

	for comp in comps:
		print(f'{comp["title"]} -> Price: {comp["price"]} -> Link: {comp["link"]}')

parse()


на сайте есть тег p с классом price, но все равно выдается такая ошибка;

Traceback (most recent call last):
  File "pars2.py", line 26, in <module>
    parse()
  File "pars2.py", line 19, in parse
    'price': item.find('p', class_ = 'price').get_text(strip = True),
AttributeError: 'NoneType' object has no attribute 'get_text'
  • Вопрос задан
  • 782 просмотра
Решения вопроса 1
hottabxp
@hottabxp Куратор тега Python
Сначала мы жили бедно, а потом нас обокрали..
Скорее всего сначала идут топовые объявления, и у них может быть другой класс (не разбирался).
Можно просто добавить try...except:
for item in items:
    try:
        comps.append({
          'title': item.find('a', class_ = 'marginright5 link linkWithHash detailsLink').get_text(strip = True),
          'price': item.find('p', class_ = 'price').get_text(strip = True),
          'link': item.find('a', class_ = 'marginright5 link linkWithHash detailsLink').get('href')
        })
    except:
        pass

  for comp in comps:
    print(f'{comp["title"]} -> Price: {comp["price"]} -> Link: {comp["link"]}')

parse()
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы