推荐学习书目
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 · 3023 views
    This topic created in 2519 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
        1
    Govn  
    OP
       Jun 20, 2019
    <em>零基础学<font class="skcolor_ljg">Python</font>(全彩版)</em>
    没写全。。
    JCZ2MkKb5S8ZX9pq
        2
    JCZ2MkKb5S8ZX9pq  
       Jun 20, 2019 via iPhone
    em//text()
    这样行嘛?
    limuyan44
        3
    limuyan44  
       Jun 21, 2019
    chrome 控制台右键 copy wiht xpath 这个没给文档结构写了哪里准哦
    ksedz
        4
    ksedz  
       Jun 21, 2019   ❤️ 1
    normalize-space(.//em)
    lxychn
        5
    lxychn  
       Jun 21, 2019   ❤️ 1
    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
        6
    Govn  
    OP
       Jun 21, 2019
    @JCZ2MkKb5S8ZX9pq 不行的呀,这样提取不出来 python。
    darknoll
        7
    darknoll  
       Jun 21, 2019
    用个正则表达式得了
    wudamen
        8
    wudamen  
       Jun 21, 2019
    from lxml import etree

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

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