exploitcat
V2EX  ›  问与答

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

  •  1
     
  •   exploitcat · Jan 14, 2016 · 4764 views
    This topic created in 3948 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
    leisurelylicht
    Jan 14, 2016
    1
    from lxml import etree

    或者

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