【问题标题】:Getting text from multiple divs with spans inside - XPath从多个具有跨度的 div 中获取文本 - XPath
【发布时间】: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


    【解决方案1】:

    尝试使用XPath下面的表达式:

    //div[@itemprop="recipeIngredient"]/string(normalize-space())
    

    【讨论】:

    • 它不编译为 xpath,而且,修改给了我:3 汤匙 特级初榨橄榄油 10 中等比利时菊苣,纵向减半 1/2 杯 金酒 盐和现磨黑胡椒
    • 我很确定这是一个有效的XPath 表达式并且它返回所需的值...您可以使用freeformatter.com/xpath-tester.html 检查它。您使用的Python 库是什么?
    • 这是编译的布局: //div[@itemprop="recipeIngredient"][string(normalize-space())]
    • 是的,但它的含义完全不同......你的XPath有什么例外吗?
    • 我正在使用 FireFox Xpath 扩展,它显示“语法错误”
    【解决方案2】:

    最后我不得不使用python,在原来的xpath上有一点循环和更多的xpath:

    if response.xpath('//*[@itemprop="recipeIngredient"]'):
        ingredients = []
        for item in response.xpath('//div[@itemprop="recipeIngredient"]'):
            item = item.xpath("span/text()").extract()
            item = " ".join([" ".join(elem.split()) for elem in item])
            ingredients.append(item)
    
        raw_recipe["ingredients"] = ingredients
    

    结果(有多余的空格,但我不介意):

    ["3 tablespoons extra-virgin olive oil ", "10 medium Belgian endives, halved lengthwise ", "1/2 cup gin ", " Salt and freshly ground black pepper ", "1 cup fresh orange juice ", "4 tablespoons unsalted butter ", "2 tablespoons honey ", "2 scallions, white and pale green parts only, thinly sliced ", "2 tablespoons salted roasted pumpkin seeds ", " Balsamic vinegar, for drizzling "]
    

    【讨论】:

      【解决方案3】:

      使用下面的 jQuery 脚本:

      var $=jQuery;
      var list=[];
      $('.field-ingredients').each(function () {
        var ingredient=[]
        $(this).find('span').each(function () {
          ingredient.push($(this).text().trim());    
        });
        list.push(ingredient.join(" ").trim());
      });
      
      console.log(list);
      

      【讨论】:

      • 我只能使用python,这不是我网站上运行的代码,对不起。
      • 她要求用python解决,这不是一个相关的答案
      猜你喜欢
      • 2017-09-27
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多