C
C
CreativeStory2018-10-07 13:53:44
Python
CreativeStory, 2018-10-07 13:53:44

Python - How to select the right row?

When parsing a site, there is such a layout

<span class="packaging-info">
    Qty: 1+
    <span class="packing-price">$0.395</span>
</span>

I can’t figure out how to get the Qty value: 1+
I do it like this
.find('span', class_='packaging-info').text.strip()

As a result, I get the full text from the nested tags:
Qty: 1+$0.395
I understand that I will not get another for such a request, how to properly separate the first value in such nested structures?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
igorzakhar, 2018-10-07
@igorzakhar

>>> body = """
... <span class="packaging-info">
...     Qty: 1+
...     <span class="packing-price">$0.395</span>
... </span>
... """
>>> soup = BeautifulSoup(body, 'lxml')
>>> elem = soup.find('span', class_='packaging-info')
>>> elem.next_element.strip()
'Qty: 1+'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question