【问题标题】:How can I get the text with xPath between </ul> and <p>?如何在 </ul> 和 <p> 之间获取带有 xPath 的文本?
【发布时间】:2014-11-26 14:45:34
【问题描述】:

我有 HTML 代码,我想用 xPath 解析以“倒入所有成分”开头的字符串。我已经用 span 和 li 对象完成了这个技巧。但是这个文本不属于任何东西。 xpath应该怎么写?

李的EG:

for ingredients in doc.xpath("//div[@class='oc_info']/ul[@class='list']/li"):
    print ingredients.text

这是 HTML:

<div class="oc_info" style="float: left; width: 400px; color: #666666; font-size: 12px; line-height:     16px;">
<p class="info"><span class="info1" style="color:#818915; font-size:16px; font-weight:bold;">CASINO</span> <br><span class="info2" style="color:black; font-size:12px; font-weight:bold;">All Day Cocktail</span></p>
<ul class="list">
<li>4 cl Old Tom Gin</li>
<li>1 cl Maraschino</li>
<li>1 cl Orange Bitters</li>
<li>1 cl Fresh lemon juice</li>
</ul>
Pour all ingredients into shaker with ice cubes, shake well, strain into chilled cocktail glass and     garnish with a lemon twist and a maraschino cherry.
<p>&nbsp;</p>
</div>
</div>

【问题讨论】:

    标签: python html xpath lxml


    【解决方案1】:

    您可以使用此 XPath 直接选择“Pour all ingredients...”文本:

    //div[@class='oc_info']/ul[@class='list']/following-sibling::text()
    

    【讨论】:

      猜你喜欢
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 2019-11-01
      • 2017-09-07
      • 1970-01-01
      相关资源
      最近更新 更多