Python находит индекс тега в строке - PullRequest
0 голосов
/ 14 января 2012

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>

код

def GetBullets(self, Soup):

    bulletList = []

    bullets = str(Soup.findAll('div', {'class': 'productDescriptionWrapper'}))

    bullets_re = re.compile('<li>(.*)</li>')

    bullets_pat = str(re.findall(bullets_re, bullets))

    index = bullets_pat.findall('</li>')

    print index

как извлечь p теги и li теги? Спасибо!

Ответы [ 2 ]

3 голосов
/ 14 января 2012

Обратите внимание на следующее:

>>> 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.

0 голосов
/ 14 января 2012

Мы используем Красивый суп для этого.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...