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
prasanta
V2EX  ›  Python

htmlparsing0.1.0 release! 添加类似 restful 的语法解析网页.

  •  
  •   prasanta · 2018-02-28 11:16:17 +08:00 · 836 次点击
    这是一个创建于 2221 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Github: https://github.com/gaojiuli/htmlparsing

    解析列表

    import requests
    from htmlparsing import Element, HTMLParsing, Text, Attr, Parse, HTML, Markdown
    
    url = 'https://news.ycombinator.com/'
    r = requests.get(url)
    article_list = HTMLParsing(r.text).list('.athing', {'title': Text('a.storylink'), # css selector
                                                        'link': Attr('a.storylink', 'href')})
    print(article_list)
    
    

    解析详情

    import requests
    from htmlparsing import Element, HTMLParsing, Text, Attr, Parse
    
    url = 'https://news.ycombinator.com/item?id=16476454'
    r = requests.get(url)
    article_detail = HTMLParsing(r.text).detail({'title': Text('a.storylink'),
                                                 'points': Parse('span.score', '>{} points'),
                                                 'link': Attr('a.storylink', 'href')})
    print(article_detail)
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2849 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:30 · PVG 22:30 · LAX 07:30 · JFK 10:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.