Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from HTMLParser import HTMLParser
- class MyHtmlParser(HTMLParser):
- '''
- Parse simple url to extract data and image url.
- This is expecting a simple url containing only one data block and one iimage url.
- '''
- def handle_starttag(self, tag, attrs):
- if tag == 'img':
- for a in attrs:
- if a[0] == 'src':
- self.imageUrl = a[1]
- def handle_data(self, data):
- self.text = data
- class HtmlIconLabel(QWidget):
- def __init__(self, parent=None):
- super(HtmlIconLabel, self).__init__(parent)
- self.setLayout(QHBoxLayout())
- self.setupUI()
- def setupUI(self):
- self.imgLabel = QLabel()
- self.textLabel = QLabel()
- self.textLabel.setTextFormat(Qt.RichText)
- self.textLabel.setWordWrap(True)
- self.textLabel.setOpenExternalLinks(True)
- self.textLabel.setStyleSheet('a:link {COLOR: #C74F24;}')
- self.layout().addWidget(self.imgLabel)
- self.layout().addWidget(self.textLabel)
- def setHtmlText(self, html, baseUrl=''):
- '''extract icon from html, download it and display it along with text contained in html'''
- import urllib2
- parser = MyHtmlParser()
- parser.feed(html)
- f = urllib2.urlopen(baseUrl + parser.imageUrl)
- imageFile = f.read()
- pixmap = QPixmap()
- pixmap.loadFromData(QByteArray(imageFile))
- self.pixmap = pixmap
- self.textLabel.setText(parser.text)
- self.imgLabel.setPixmap(self.pixmap)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement