class="ings"
인 요소 내에있는 p
요소에 대해 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)
인쇄 : 나는 또한 당신의 로케이터 조금 개선하고 div.ings p
CSS selector로 전환 한
[
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'
]
참고.