推荐学习书目
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 · Feb 28, 2018 · 1247 views
    This topic created in 3085 days ago, the information mentioned may be changed or developed.

    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)
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5892 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 03:07 · PVG 11:07 · LAX 20:07 · JFK 23:07
    ♥ Do have faith in what you're doing.