python在字符串中查找标记的索引

2024-09-30 08:16:45 发布

您现在位置:Python中文网/ 问答频道 /正文

HTML格式

<div class="productDescriptionWrapper">
<p>A worm worth getting your hands dirty over. With over six feet of crawl space, Playhut&rsquo;s Wiggly Worm is a brightly colored and friendly play structure.
</p>
<ul>  
   <li>6ft of crawl through fun</li>    
   <li>18&rdquo; diameter for easy crawl through</li>    
   <li>Bright colorful design</li>    
   <li>Product Measures: 18&quot;&quot;Diam x 60&quot;&quot;L</li>    
   <li>Recommended Ages: 3 years &amp; up<br />    &nbsp;</li>
</ul>
<p><strong>Intended for Indoor Use</strong></p>

代码

^{pr2}$

如何提取p标记和li标记?谢谢!在


Tags: of标记divforhtml格式liul
2条回答

注意以下几点:

>>> from BeautifulSoup import BeautifulSoup
>>> html = """ <what you have above> """
>>> Soup = BeautifulSoup(html)
>>> bullets = Soup.findAll('div', {'class': 'productDescriptionWrapper'})
>>> ptags = bullets[0].findAll('p')
>>> print ptags
[<p>A worm worth getting your hands dirty over. With over six feet of crawl space,      Playhut&rsquo;s Wiggly Worm is a brightly colored and friendly play structure.
</p>, <p><strong>Intended for Indoor Use</strong></p>]
>>> print ptags[0].text
A worm worth getting your hands dirty over. With over six feet of crawl space, Playhut&rsquo;s Wiggly Worm is a brightly colored and friendly play structure.

你可以用类似的方式获取你的li标签的内容。在

我们用Beautiful Soup来表示。在

相关问题 更多 >

    热门问题