V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2017-01-04 19:04:38 +08:00 · 3712 次点击
    这是一个创建于 2661 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近需要用 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 条回复    2017-01-05 09:05:47 +08:00
    tairan2006
        1
    tairan2006  
       2017-01-04 19:07:21 +08:00
    来吧 龙书参上
    Septembers
        2
    Septembers  
       2017-01-04 22:13:44 +08:00
    嗯 来个简单点的吧解析 JSON 并且通过 http://seriot.ch/parsing_json.php 所有 case 再试试 HTML

    HTML5 Testsuite
    https://github.com/html5lib/html5lib-tests
    yzhen123
        3
    yzhen123  
       2017-01-05 08:43:03 +08:00
    是 xml 格式的吗?是的话你需要的应该是 xml 解析库。。
    canaan
        4
    canaan  
       2017-01-05 09:02:17 +08:00
    searene
        5
    searene  
    OP
       2017-01-05 09:05:47 +08:00
    @yzhen123 不是 xml 格式
    @canaan 谢谢!看起来正是我所需要的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5383 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 03:37 · PVG 11:37 · LAX 20:37 · JFK 23:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.