开发者

python html parsing

开发者 https://www.devze.com 2023-03-09 13:10 出处:网络
i need do some html parsing use python .if i have a html file like bellow: 《body》 《div class=\"mydiv\"》

i need do some html parsing use python .if i have a html file like bellow:

《body》
   《div class="mydiv"》
      《p》i want got it《/p》
      《div》
           《p》 good 《/p》
           《a》 boy  《/a》
      《/div》
   《/div》
《/body》

how can i get the content of 《div class="mydiv"》 ,say , i want got .

      《p》i want got it《/p》
      《div》
           《p》 good 《/p》
           《a》 boy 《/a》
      《/div》

i have try HTMLParser, but i f开发者_运维问答ount it can't. anyway else ? thanks!


With BeautifulSoup it is as simple as:

from BeautifulSoup import BeautifulSoup
    html = """
      <body>
        <div class="mydiv">
          <p>i want got it</p>
          <div>
            <p> good </p>
            <a> boy  </a>
          </div>
        </div>
      </body>
    """

    soup = BeautifulSoup(html)
    result = soup.findAll('div', {'class': 'mydiv'})
    tag = result[0]
    print tag.contents
    [u'\n', <p>i want got it</p>, u'\n', <div>
    <p> good </p>
    <a> boy  </a>
    </div>, u'\n']


Use lxml. Or BeautifulSoup.


I would prefer lxml.html.

import lxml.html as H
doc  = H.fromstring(html)
node = doc.xpath("//div[@class='mydiv']")
0

精彩评论

暂无评论...
验证码 换一张
取 消