0

Решил написать парсер, код ниже, и при парсинге картинки выдаёт ошибку, как исправить, пишу по гайду, у человека в гайде все получилось, помогите пожалуйста

Ошибка: "AttributeError: 'NoneType' object has no attribute 'get'"

import requests
import csv

HOST = 'https://magiclesson-shop.ru'
URL = 'https://magiclesson-shop.ru/collection/karty-bicycle'
HEADERS = {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'}

def get_html(url, params = ''):
    r = requests.get(url, headers = HEADERS, params=params)
    return r

def get_content(html):
    soup = BeautifulSoup(html, 'html.parser')
    items = soup.find_all('div', class_ = 'product_preview')
    cards = []

    for item in items:
        cards.append(
            {
                'tittle': item.find('div', class_='product_preview-title').get_text(),
                'link_product': item.find('div', class_='product_preview-title').find('a').get('href'),
                'price': item.find('div', class_='prices-current').get_text(),
                'img_product': item.find('div', class_='product_preview-title').find('img').get('src')

            }
        )

    return cards

html = get_html(URL)
print(get_content(html.text))

1 ответ 1

0

Вы просто перепутали классы

HTML картинки находится в классе product_preview-preview, а не product_preview-title

Вам стоит заменить строку

'img_product': item.find('div', class_='product_preview-title').find('img').get('src')

на

'img_product': item.find('div', class_='product_preview-preview').find('img').get('src')

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.