Ilmo Ahorinta
Ilmo Ahorinta

Reputation: 3

How to get string from span tag in beautifulsoup

I have a problem with getting the string out of span tag in beautifulsoup. I tried using the text attribute but that gave me an AttributeError: NoneType object has no attribute text.

source = requests.get("https://www.k-ruoka.fi/kauppa/tuotehaku").text

soup = BeautifulSoup(source, "lxml")


product = soup.find("ul", class_="product-grid")


for listt in product.find_all("li"):
    kg = listt.find("span", class_="reference").text
    print(kg)

The code above gives the AttributeError. if i dont use .text then it gives me this:

<span class="reference">1,58<span class="slash">/</span>kg</span>

but I just want the "1,58" and "kg" from it.

Upvotes: 0

Views: 430

Answers (2)

Xay
Xay

Reputation: 276

Your code works, all you need is a check for NoneType in the for-loop:

for listt in product.find_all("li"):
    kg = listt.find("span", class_="reference")
    if kg:
        print(kg.text)

Upvotes: 2

Prayson W. Daniel
Prayson W. Daniel

Reputation: 15558

Xay is correct. The issue was your first element was NoneType. NoneType.text threw that error. Another way you to what Xay suggested is:

import requests
from bs4 import BeautifulSoup

source = requests.get("https://www.k-ruoka.fi/kauppa/tuotehaku").text

soup = BeautifulSoup(source, "lxml")



product = soup.find("ul", class_="product-grid")

kgs = [listt.find("span", class_="reference").text for listt in product.find_all("li") 
           if listt.find("span", class_="reference")]

for kg in kgs:
    print(kg)

So what I did to get kgs is to ask for text only if listt.find("span", class_="reference") cought something. None is False so, it will not be collected.

Upvotes: 1

Related Questions