I want to extract the price value from a block of html that looks like this:
<div class="price">
<span>
<span class=""><span class="value"></span></span>
<span class=" list">
<span class="value">$129.99</span>
</span>
</span>
</div>
I tried price = soup.find("span", {"class": "value"}).contents[1]
thinking that would give me the contents of the second span
with a class of value
however I get an error saying list index out of range
soup.find
returns the FIRST matching item. You're thinking of find_all
. Or, you could use an XPATH expression like div/span/span[2]/span
.
Did you mean this?
price = soup.find_all("span", {"class": "value"})[1].text
Inner span with class='value'
is not really children of the first span, so .contents
[docs] will not give you desired value. If you want to grab content from upper span tag for some reason, you can use .find_next()
to move on to the next tag and grab its content:
price = soup.find("span", {"class": "value"}).find_next().get_text().strip()
# '$129.99'
soup.find("span", {"class": "list"}).find("span", {"class": "value"}).text
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.