【发布时间】:2017-02-23 11:31:02
【问题描述】:
我正在尝试从该站点提取成分(仅使用 python、scrapy 和 xpath):
http://www.myrecipes.com/recipe/gin-orange-juice-braised-endives
我使用以下 xpath:
//*[@itemprop="recipeIngredient"]/descendant-or-self::*/text()
我需要像这样的清单:
["3 tablespoons extra-virgin olive oil",
"10 medium Belgian endives, halved lengthwise",
"1/2 cup gin",
"Salt and freshly ground black pepper"
...]
但它给了我很多空间:
[u'\n ', u'3 tablespoons', u'\n ', u' \n extra-virgin olive oil\n ', u'\n ', u' ', u'\n', u'\n ', u'10 ', u'\n ', u' \n medium Belgian endives, halved lengthwise\n ', u'\n ', u' ', u'\n', u'\n ', u'1/2 cup', u'\n ', u' \n gin\n ', u'\n ', u' ', u'\n', u'\n ', u' ', u'\n ', u' \n Salt and freshly ground black pepper\n ', u'\n ', u' ', u'\n', u'\n ', u'1 cup', u'\n ', u' \n fresh orange juice\n ', u'\n ', u' ', u'\n', u'\n ', u'4 tablespoons', u'\n ', u' \n unsalted butter\n ', u'\n ', u' ', u'\n', u'\n ', u'2 tablespoons', u'\n ', u' \n honey\n ', u'\n ', u' ', u'\n', u'\n ', u'2 ', u'\n ', u' \n scallions, white and pale green parts only, thinly sliced\n ', u'\n ', u' ', u'\n', u'\n ', u'2 tablespoons', u'\n ', u' \n salted roasted pumpkin seeds\n ', u'\n ', u' ', u'\n', u'\n ', u' ', u'\n ', u' \n Balsamic vinegar, for drizzling\n ', u'\n ', u' ', u'\n']
用python(2.7)剥离每个项目后:
["3 tablespoons",
"extra-virgin olive oil",
"10",
"medium Belgian endives, halved lengthwise",
"1/2 cup",
"gin",
"Salt and freshly ground black pepper",
...]
每种成分都在一个 div 中,如下所示:
<div itemprop="recipeIngredient" >
<span>3 tablespoons</span>
<span>
extra-virgin olive oil
</span>
<span> </span>
</div>
如果我使用 normalize-text,像这样:
normalize-space(//*[@itemprop="recipeIngredient"])
我只得到这个:
3 tablespoons extra-virgin olive oil
这太棒了,但我需要所有的 div 而不仅仅是第一个。
任何帮助将不胜感激。
【问题讨论】:
标签: python-2.7 xpath scrapy