纯净的 HTML 解析库, 取代复杂的 beautifulsoup4, pyquery, lxml
github: https://github.com/gaojiuli/htmlparsing
pip install htmlparsing
# or
pip install git+https://github.com/gaojiuli/htmlparsing
import requests
from htmlparsing import Element
url = 'https://python.org'
r = requests.get(url)
e = Element(text=r.text, base_url=url)
e.links
"""
{...'/users/membership/', '/events/python-events', '//docs.python.org/3/tutorial/controlflow.html#defining-functions'}
"""
e.absolute_links
"""
{...'https://python.org/download/alternatives', 'https://python.org/about/success/#software-development', 'https://python.org/download/other/', 'https://python.org/community/irc/'}
"""
e.xpath('//a')[0].attrs
"""{'href': '#content', 'title': 'Skip to content'}"""
e.xpath('//a')[0].attrs.title
"""Skip to content"""
e.css('a')[0].attrs
"""{'href': '#content', 'title': 'Skip to content'}"""
e.parse('<a href="#content" title="Skip to content">{}</a>'))
"""<Result ('Skip to content',) {}>"""
e.xpath('//a')[5].text
"""PyPI"""
e.xpath('//a')[5].html
"""<a href="https://pypi.python.org/" title="Python Package Index">PyPI</a>"""
e.xpath('//a')[5].markdown
"""[PyPI]( https://pypi.python.org/ "Python Package Index")"""
目前支持的选择器: xpath, css ,parse
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.