问题
I am scraping a website and would like to create a list of prices.
prices = soup.find_all("li", class_="price")
However, this returns:
<li class="price">€13.99</li>,
<li class="price">€12.99</li>,
.....
How do I extract just the price? I tried
prices = soup.find_all("li", class_="price", text=True)
but it did not work.
I know I can go through the list manually and extract the text but this isn't ideal.
回答1:
Assuming content is not dynamically added, which it appears it is not, I would use .text to extract from elements returned by using select
prices = [item.text for item in soup.select('li.price')]
回答2:
find_all() returns list of element.You need to iterate this to get each element and then get the text of the element.
prices = soup.find_all("li", class_="price", text=True)
for price in prices:
print(price.text)
来源:https://stackoverflow.com/questions/56860559/bs4-beautiful-soup-extract-text-from-find-all