Python中的Html元素位置

muc*_*out 2 html python lxml

我正在使用lxml.html在python中进行一些html解析.我希望粗略估计页面中的元素在浏览器呈现后的位置.它不一定非常精确,但通常是正确的.为简单起见,我将忽略Javascript对元素位置的影响.作为最终结果,我希望能够迭代元素(例如,通过lxml)并找到它们的x/y坐标.有关如何做到这一点的任何想法?我不需要继续使用lxml,我很乐意尝试其他库.

Kab*_*bie 5

使用webkit的PyQt:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

class MyWebView(QWebView):
    def __init__(self):
        QWebView.__init__(self)
        QObject.connect(self,SIGNAL('loadFinished(bool)'),self.showelements)

    def showelements(self):
        html=self.page().currentFrame().documentElement()
        for link in html.findAll('a'):
            print(link.toInnerXml(),str(link.geometry())[18:])


if __name__=='__main__':
    app = QApplication(sys.argv)

    web = MyWebView()
    web.load(QUrl("http://www.google.com"))
    web.show()

    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)