如何使用美丽的汤获取儿童标签描述的文本

时间:2016-08-30 13:38:44

标签: python beautifulsoup html-parsing

我正在使用美丽的汤来榨取一些数据 foodily.com

在上面的页面中有一个div,其中包含类'ings',我希望在p标记中获取数据,我已在下面编写代码:

ingredients = soup.find('div', {"class": "ings"}).findChildren('p')

它提供了成分列表,但带有p标签。

3 个答案:

答案 0 :(得分:2)

使用pdiv元素内的每个class="ings"元素调用get_text()

完整的工作代码:

from bs4 import BeautifulSoup
import requests

with requests.Session() as session:
    session.headers.update({"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36"})
    response = session.get("http://www.foodily.com/r/0y1ygzt3zf-perfect-vanilla-cupcakes-by-annie-s")

    soup = BeautifulSoup(response.content, "html.parser")

    ingredients = [ingredient.get_text() for ingredient in soup.select('div.ings p')]
    print(ingredients)

打印:

[
    u'For the cupcakes:', 
    u'1 stick (113g) butter/marg*', 
    u'1 cup caster sugar', u'2 eggs', 
    ...
    u'1 tbsp vanilla extract', 
    u'2-3tbsp milk', 
    u'Sprinkles to decorate, optional'
]

请注意,我还改进了您的定位器并切换到div.ings p CSS selector

答案 1 :(得分:0)

另一种方式:

import requests
from bs4 import BeautifulSoup as bs


url = "http://www.foodily.com/r/0y1ygzt3zf-perfect-vanilla-cupcakes-by-annie-s"
source = requests.get(url)
text_new = source.text
soup = bs(text_new, "html.parser")
ingredients  = soup.findAll('div', {"class": "ings"})
for a in ingredients :
    print (a.text)

它将打印:

For the cupcakes:

1 stick (113g) butter/marg*

1 cup caster sugar

2 eggs

1 tbsp vanilla extract

1 and 1/2 cups plain flour

2 tsp baking powder

1/2 cup milk (I use Skim)

For the frosting:

2 sticks (226g) unsalted butter, at room temp

2 and 1/2 cups icing sugar, sifted

1 tbsp vanilla extract

2-3tbsp milk

Sprinkles to decorate, optional

答案 2 :(得分:0)

如果您已拥有p代码列表,请使用get_text()。这将只返回它们的文本:

ingredient_list = p.get_text() for p in ingredients

结果数组如下所示:

ingredient_list = [
   'For the cupcakes:', '1 stick (113g) butter/marg*', 
   '1 cup caster sugar','2 eggs', ...
]