Python, не работает парсер
написала парсер на языке программирования Python (3.8), по неизвестной мне причине ничего не отображается, даже ошибки.
Вот мой код:
from bs4 import BeautifulSoup
import requests
def parse():
URL = 'https://www.wildberries.ru/catalog/muzhchinam/odezhda/vodolazki'
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_ = 'dtList-innerr')
comps = []
for item in items:
try:
comps.append({
'title': item.find('span', class_ = 'goods-name c-text-sm').get_text(strip = True),
'price': item.find('span', class_ = 'price').get_text(strip = True),
'link': item.find('a', class_ = 'ref_goods_n_p j-open-full-product-card').get('href')
})
except:
pass
global comp
for comp in comps:
print(f'{comp["title"]} -> Price: {comp["price"]} -> Link: {comp["link"]}')
parse()
Ответы (2 шт):
Автор решения: icYFTL
→ Ссылка
Ищите проблему в items или в блоке try отладкой или:
from bs4 import BeautifulSoup
import requests
def parse():
URL = 'https://www.wildberries.ru/catalog/muzhchinam/odezhda/vodolazki'
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_ = 'dtList-innerr')
comps = []
print(items) # Pseudo-debug
for item in items:
try:
comps.append({
'title': item.find('span', class_ = 'goods-name c-text-sm').get_text(strip = True),
'price': item.find('span', class_ = 'price').get_text(strip = True),
'link': item.find('a', class_ = 'ref_goods_n_p j-open-full-product-card').get('href')
})
except Exception as e:
print(str(e)) # Pseudo-debug
global comp
for comp in comps:
print(f'{comp["title"]} -> Price: {comp["price"]} -> Link: {comp["link"]}')
parse()
Автор решения: CrazyElf
→ Ссылка
У вас всего лишь ошибка в названии класса, который вы ищете - лишняя буква r в конце.
Вот с таким названием класса всё работает: dtList-inner.
Полезно всё-таки знать английский язык.
Ну и проверять, что вообще ищет ваш код руками в response (а есть ли это вообще там) - тоже полезно.
items = soup.findAll('div', class_ = 'dtList-inner') # удалил лишнюю букву