Проблема с парсингом картинок
Проблема с парсингом картинок заключается в том что картинки не вытаскиваются нормально, при попытке запуска кода выдается ошибка AttributeError: 'NoneType' object has no attribute 'get'
но следует только убрать строку которая должна собирать картинки то код начинает нормально работать
import requests
from bs4 import BeautifulSoup
URL = 'https://rimax-industry.en.alibaba.com/productlist-1.html'
LINK = 'https://rimax-industry.en.alibaba.com/'
HEADERS = {
'accept': '*/*',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/92.0.4515.131'
}
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_='icbu-product-card vertical large product-item')
items = items + soup.find_all('div', class_='icbu-product-card vertical large product-item last')
disks = []
for item in items:
disks.append(
{
'title': item.find('div', class_='title').get_text(),
'link_prod': LINK + item.find('a', class_='product-image').get('href'),
'minitems': item.find('span', class_='value').get_text(),
'price': item.find('span', class_='num').get_text(),
'img_product': item.find('a', class_='product-image').find('img').get('src')
}
)
return disks
html = get_html(URL)
print(get_content(html.text))
Но следует оставить строку со сбором картинок и заменить
items = soup.find_all('div', class_='icbu-product-card vertical large product-item')
items = items + soup.find_all('div', class_='icbu-product-card vertical large product-item last')
на
items = soup.find_all('div', class_='component-product-list')
то есть просто повысить иерархию классов то получается получить все указанные данные но только по одному товару
Ответы (1 шт):
Ошибка в том, что в указанной строке, происходит поиск тега 'img' и 'product-image' и очевидно не найдя оного, бс4 возвращает тип none. К которому происходит обращение, как будто это действительный тег. Вероятно замена поможет
img_product=item.find('a', class_='product-image').find('img')
if img_product is bs.element.Tag:
'all ok'
img_product.get('src')
else:
'no result'