BS4 Beautiful Soup extract text from find_all

点点圈 提交于 2021-02-04 08:28:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!