Не идет парсинг в BeautifulSoup на python 3
В общем этот код должен парсить название продуктов но в консоли выводит пустую строку. Хочу узнать почему не работает и заранее спасибо.
from bs4 import BeautifulSoup as bs
import requests
def parse():
URL = 'https://haribo-shop.ru/catalog/zhevatelnie_konfety/'
HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0'}
response = requests.get(URL, headers = HEADERS)
soup = bs(response.content, 'html.parser')
items = soup.findAll('div', class_ = 'item ')
comps = []
for item in items:
comps.append({'title': item.find('a', class_ = 'name').get_text(strip = True)})
for comp in comps:
print(comp['title'])
parse()
Ответы (1 шт):
Автор решения: RomanR
→ Ссылка
Почему то в строке items = soup.findAll('div', class_ = 'item ') вы пытаетесь найти класс "item", хотя нужен 'div', class_='name-cont part'
Класса item, там вообще не видно, но может и просмотрел
def parse():
URL = 'https://haribo-shop.ru/catalog/zhevatelnie_konfety/'
HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0'}
response = requests.get(URL, headers = HEADERS)
soup = bs(response.content, 'html.parser')
items = soup.find_all('div', class_='name-cont part')
comps = []
for item in items:
comps.append({'title': item.find('div', class_ = 'name').get_text(strip = True)})
for comp in comps:
print(comp['title'])
parse()
Но вообще можно сразу искать и class_ = 'name'
def parse():
URL = 'https://haribo-shop.ru/catalog/zhevatelnie_konfety/'
HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0'}
response = requests.get(URL, headers = HEADERS)
soup = bs(response.content, 'html.parser')
names = soup.find_all('div', class_='name')
comps = []
for name in names:
comps.append({'title': name.get_text(strip = True)})
for comp in comps:
print(comp['title'])
parse()