При сборе данных из всех карточек на сайте и в дальнейшем сборе данных из них вылетает ошибка
ResultSet object has no attribute 'find'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
import requests
headers={
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'accept': '*/*'
}
url="https://www.holodilnik.ru/smartphones_gadgets/smartphones/?page=1"
responce = requests.get(url, headers=headers)
soup = BeautifulSoup(responce.text, 'lxml')
data = soup.find_all("div", class_="col-12")
for i in data:
name = data.find("span", itemprop="name").text
price = data.find("div", class_="price").text
print(name + ": " + price)
Ответы (1 шт):
Автор решения: DiMithras
→ Ссылка
AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
Если прочесть ошибку всё более чем очевидно.
Ты объявляешь цикл через for i in data, а затем обращаешься к data. Зачем тебе цикл тогда?
Надо так:
for i in data:
name = i.find("span", itemprop="name").text #<====== i вместо data
price = i.find("div", class_="price").text #<====== i вместо data
print(name + ": " + price)
А это тебя приведёт к следующей ошибке:
AttributeError: 'NoneType' object has no attribute 'text'
Поэтому надо проверять есть нахождение или нет!
for i in data:
name = i.find("span", itemprop="name")
price = i.find("div", class_="price")
if name != None and price != None:
print("{}: {}".format(name.text,price.text))