search

Home  >  Q&A  >  body text

A little question about xpath in Python

There is a piece of html code as follows:


<p class="col-9 d-inline-block text gray mb-2 pr-4">
My <em>Python</em> Examples
</p>

What should I do if I want to use xpath to extract My Python Examples

过去多啦不再A梦过去多啦不再A梦2780 days ago914

reply all(2)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-18 10:59:45

    Read more help documents!

    //p[@class="col-9 d-inline-block text gray mb-2 pr-4"]/text()

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-18 10:59:45

    Refer to this answer of mine:/q/10...

    But I discovered a new method, p_dom.xpath("string()"),可以直接取出内容作为一个完整的字符串;
    然后就是lxml.etree中的方法:etree.tostring(p_dom, method='text'), but you still have to choose according to your needs.

    reply
    0
  • Cancelreply