Created
September 9, 2019 13:34
-
-
Save amirouche/6b03647618bc132c8e7b738040541e43 to your computer and use it in GitHub Desktop.
pyqt5 webkit + lxml example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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