exploitcat
V2EX  ›  问与答

Python 中哪个库可以获取一个网页元素的 XPath?

  •  1
     
  •   exploitcat · Jan 14, 2016 · 4391 views
    This topic created in 3797 days ago, the information mentioned may be changed or developed.

    最近有个需求,就是拿到一个网页的源代码,然后找到其中元素对应的 XPath ,在 Python 中完成。
    类似于 Chrome 中的"Copy XPath"功能。
    伸手党想问一下有没有现成的 Python 库可以解决?

    8 replies    2019-12-10 13:53:08 +08:00
    leisurelylicht
        1
    leisurelylicht  
       Jan 14, 2016
    from lxml import etree

    或者

    beautifulsoup
    exploitcat
        2
    exploitcat  
    OP
       Jan 14, 2016
    @leisurelylicht 有没有 demo 呢? 可以获取指定 element 的 xpath ?
    bdbai
        3
    bdbai  
       Jan 14, 2016 via iPhone
    楼上说的是通过 XPath 选择元素吧。
    为什么要用 Python 生成 XPath ?如何确定是哪一个元素?
    imn1
        4
    imn1  
       Jan 14, 2016
    同一元素 xpath 有多种写法
    最简单的方法是你自己递归父节点,直到 root 节点
    exploitcat
        5
    exploitcat  
    OP
       Jan 14, 2016
    @bdbai 需求往往不需要解释。。。。
    leisurelylicht
        6
    leisurelylicht  
       Jan 15, 2016
    @exploitcat 好像没有,需要你自己生成
    wwxiong
        7
    wwxiong  
       Jan 15, 2016
    @exploitcat 可以看看 https://github.com/wwxiong/spiders/blob/master/meizi_spider.py 爬虫。里面就是使用 lxml 。
    ruiruirui
        8
    ruiruirui  
       Dec 10, 2019
    楼主是如何解决这个问题的,我现在也有个这么一个需求。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   836 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 20:09 · PVG 04:09 · LAX 13:09 · JFK 16:09
    ♥ Do have faith in what you're doing.