推荐学习书目
› 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
Govn
V2EX  ›  Python

xpath 问题相关求回答!谢谢大佬们。

  •  
  •   Govn ·
    DDFlaybird · Jun 20, 2019 · 3466 views
    This topic created in 2700 days ago, the information mentioned may be changed or developed.

    零基础学<font class="skcolor_ljg">Python</font>(全彩版) 怎么吧这个标签里的汉字用 xpath 全部提取出来。 谢谢大佬们。

    15 replies  •  2019-06-21 17:27:31 +08:00
    Govn
    Govn
    OP
    Jun 20, 2019
    1
    <em>零基础学<font class="skcolor_ljg">Python</font>(全彩版)</em>
    没写全。。
    JCZ2MkKb5S8ZX9pq
    JCZ2MkKb5S8ZX9pq
    Jun 20, 2019 via iPhone
    2
    em//text()
    这样行嘛?
    limuyan44
    limuyan44
    Jun 21, 2019
    3
    chrome 控制台右键 copy wiht xpath 这个没给文档结构写了哪里准哦
    ksedz
    ksedz
    Jun 21, 2019❤️1
    4
    normalize-space(.//em)
    lxychn
    lxychn
    Jun 21, 2019❤️1
    5
    function getElementByXpath(path) {
    return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    }

    var text = getElementByXpath("//font[@class='skcolor_ljg']/text()")
    Govn
    Govn
    OP
    Jun 21, 2019
    6
    @JCZ2MkKb5S8ZX9pq 不行的呀,这样提取不出来 python。
    darknoll
    darknoll
    Jun 21, 2019
    7
    用个正则表达式得了
    wudamen
    wudamen
    Jun 21, 2019
    8
    from lxml import etree

    html = '<em>零基础学<font class="skcolor_ljg">Python</font>(全彩版)</em>'

    print(etree.HTML(html).xpath('string(//em)'))
    零基础学 Python (全彩版)
    R0n1n
    R0n1n
    Jun 21, 2019 via Android
    9
    用正则,这样就得到两个问题啦🤣
    Govn
    Govn
    OP
    Jun 21, 2019
    10
    @R0n1n 是的 我决定用正则了。
    Govn
    Govn
    OP
    Jun 21, 2019
    11
    @darknoll 是的,我决定用正则了。
    Govn
    Govn
    OP
    Jun 21, 2019
    12
    其实昨天试了一下用 xpath ("string(//font[@class='skcolor_ljg'])")可以提取出来。
    foxyier
    foxyier
    Jun 21, 2019
    13
    //em/text()[2]
    foxyier
    foxyier
    Jun 21, 2019
    14
    修正楼上。把[2]去掉。
    dsg001
    dsg001
    Jun 21, 2019❤️1
    15
    >>> from lxml import etree
    >>> html = '<em>零基础学<font class="skcolor_ljg">Python</font>(全彩版)</em>'
    >>> doc = etree.fromstring(html)
    >>> doc.xpath('//em//text()')
    ['零基础学', 'Python', '(全彩版)']
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2132 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 00:10 · PVG 08:10 · LAX 16:10 · JFK 19:10
    ♥ Do have faith in what you're doing.