BeautifulSoup Парсер элементов
Подскажите пожалуйста, как спарсить div idэлемента c15597187
<div id="c15597187" class="l_class">
<a class="ref_goods_n_p j-open-full-product-card" href="/catalog/15597187/detail.aspx?targetUrl=GP" itemscope="" itemtype="http://schema.org/ImageObject">
или href="/catalog/15597187/detail.aspx?targetUrl=GP"
from bs4 import BeautifulSoup
import urllib.request
import textwrap
import csv
def cprint(text, color='def'):
str = ''
fc = '\033['
colors = {'def': '0m',
'red': '91m',
'under': '04m',
'green': '32m',
'blue': '94m',
'cyan': '96m',
'yellow': '93m',
'magenta': '95m',
'grey': '90m'
}
if color not in colors:
print(str + fc + colors['def'] + text)
else:
if color == 'under':
print(str + fc + colors['under'] + text)
else:
print(str + fc + colors[color] + text)
def parser(link):
req = urllib.request.urlopen(link)
html = req.read()
soup = BeautifulSoup(html, "html.parser")
news = soup.find_all('div', class_='dtlist-inner-brand')
for item in news:
title = item.find('div', class_='dtlist-inner-brand-name').get_text(strip=True)
price = item.find('div', class_='j-cataloger-price').get_text(strip=True)
# #href = item.find('a', {'class': 'ref_goods_n_p'}).get('href')
# #link = item.find('a', class_='ref_goods_n_p j-open-full-product-card').get('href')
# #href = soup.find_all('a', class_='ref_goods_n_p j-open-full-product-card')[0]
# href = item.find("div", {"id": "l_class"})
# print(href)
href = item.findAll("div", {"id": "c15597187"})
print(title, end=" = ")
print("\033[4m\033[37m\033[40m{}\033[0m".format(price))
print(href)
x = "https://www.wildberries.ru/catalog/knigi-i-kantstovary/kantstovary/bumazhnaya-produktsiya"
b = 1
for i in range(1):
parser(x)
b += 1
x = "https://www.wildberries.ru/catalog/knigi-i-kantstovary/kantstovary/bumazhnaya-produktsiya" + "?page=" + str(b)
Ответы (1 шт):
Автор решения: Exe
→ Ссылка
C div
soup.find("div", {"id": "c15597187"})
Или без
soup.find(id="c15597187")
Что бы найти все
soup.findAll("div", {"id": "c15597187"}):
найти несколько id
soup.findAll("div", {"id": ["c15597187","anotherID"]})
Вы также можете выбрать конкреытный блок
Имена с точкой значит класс .someclass
Без точки тег div
Если в имени есть пробел то его можно заменить точкой .some.class
id если я не ошибаюсь выберается решеткой #someid
soup.select(".ufoot > div > .post_rating > span")
Получить значение из href
soup["href"]
