推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
searene
V2EX  ›  Python

如何手写一个 HTML 解析库?

  •  
  •   searene · Jan 4, 2017 · 4579 views
    This topic created in 3414 days ago, the information mentioned may be changed or developed.

    最近需要用 python 写一个 DSL 解析器, DSL 就是一个字典格式,用过 goldendict 的人可能知道,格式与 HTML 很像,解析的方法应该与 HTML 解析器差不多,所以想知道现在比较流行的 HTML 解析器(如 python 的 beautifulsoup 和 java 的 jsoup )是如何实现这一功能的。查资料只查到了 ply 这个库,是一种 lr parser ,不知道这种方案能不能满足需求,因为我并没有找到用 ply 解析 HTML 的相关例子。

    也许看 beautifulsoup 或者 goldendict 的源码可以解决问题,不过在不明白原理的情况下确实很难看懂源码,在网上搜索出来的信息大部分也都是利用相应的库解决,并没有找到自己想要的资料,所以希望能给个关键字或者教程之类的,学习一下,谢谢。

    附上一份 dsl 样本: https://github.com/Tvangeste/SampleDSL/blob/master/sample.dsl

    5 replies    2017-01-05 09:05:47 +08:00
    tairan2006
        1
    tairan2006  
       Jan 4, 2017
    来吧 龙书参上
    Septembers
        2
    Septembers  
       Jan 4, 2017
    嗯 来个简单点的吧解析 JSON 并且通过 http://seriot.ch/parsing_json.php 所有 case 再试试 HTML

    HTML5 Testsuite
    https://github.com/html5lib/html5lib-tests
    yzhen123
        3
    yzhen123  
       Jan 5, 2017
    是 xml 格式的吗?是的话你需要的应该是 xml 解析库。。
    canaan
        4
    canaan  
       Jan 5, 2017
    searene
        5
    searene  
    OP
       Jan 5, 2017
    @yzhen123 不是 xml 格式
    @canaan 谢谢!看起来正是我所需要的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5569 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 84ms · UTC 09:05 · PVG 17:05 · LAX 02:05 · JFK 05:05
    ♥ Do have faith in what you're doing.