Skip to content

Instantly share code, notes, and snippets.

@amirouche
Created September 9, 2019 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirouche/6b03647618bc132c8e7b738040541e43 to your computer and use it in GitHub Desktop.
Save amirouche/6b03647618bc132c8e7b738040541e43 to your computer and use it in GitHub Desktop.
pyqt5 webkit + lxml example
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebKitWidgets import *
from lxml import html
#Take this class for granted.Just use result of rendering.
class Render(QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.mainFrame().load(QUrl(url))
self.app.exec_()
def _loadFinished(self, result):
self.frame = self.mainFrame()
self.app.quit()
url = 'http://pycoders.com/archive/'
r = Render(url)
result = r.frame.toHtml()
#This step is important.Converting QString to Ascii for lxml to process
archive_links = html.fromstring(str(result))
print(html.tostring(archive_links))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment