python html parse

  1. bs4:转换成unicode编码,http://www.crummy.com/software/BeautifulSoup/
    1. from bs4 import BeautifulSoup
      
      soup = BeautifulSoup(open("index.html"))
      
      soup = BeautifulSoup("<html>data</html>")
    2. Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种: Tag , NavigableString ,BeautifulSoup , Comment .
    3. from bs4 import SoupStrainer
      
      only_a_tags = SoupStrainer("a")
      
      only_tags_with_id_link2 = SoupStrainer(id="link2")
      
      def is_short_string(string):
          return len(string) < 10
      
      only_short_strings = SoupStrainer(text=is_short_string)
    4. BeautifulSoup(html_doc, "html.parser", parse_only=only_a_tags)
  2. lxml: python 对 libxml 的包装
  3. html5lib:纯python实现

posted on 2015-01-09 13:32  架构师师  阅读(664)  评论(0编辑  收藏  举报

导航