coding utf-8 from PyQt4 QtCore import QXmlStreamReader QXmlStreamEntit

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-
from PyQt4.QtCore import QXmlStreamReader, QXmlStreamEntityResolver
from PyQt4.QtCore import QObject, QUrl, QString, SIGNAL
from PyQt4.QtNetwork import QHttp
from PyQt4.QtGui import QApplication
#from chardet import detect
from urllib import urlencode
class SearchResultReader(QXmlStreamReader):
def __init__(self, http):
QXmlStreamReader.__init__(self)
self.http = http
@property
def tagName(self):
return self.name().toString()
@property
def tagValue(self):
return self.readElementText()
def read(self):
while not self.atEnd():
self.readNext()
if self.isStartElement() and \
self.tagName == 'file':
self.readResult()
if self.hasError():
print self.error()
def readResult(self):
"""
Reading search result item
"""
while not self.atEnd():
self.readNext()
print self.tagName, self.tagValue
if self.isEndElement() and \
self.tagName == 'file':
break
def addData(self):
"""
Adding data recieved by http handler
"""
data = self.http.readAll()
QXmlStreamReader.addData(self, data)
self.read()
def onStateChanged(state):
if state == 0:
sys.exit(1)
url = '/?%s' % urlencode({
'view': 'xml',
'on_page': '1',
'q': '123',
'codepage' : 'utf-8'
})
http = QHttp('www.proisk.ru')
r = SearchResultReader(http)
http.get(url)
QObject.connect(http, SIGNAL('readyRead (const QHttpResponseHeader&)'), r.addData)
QObject.connect(http, SIGNAL('stateChanged (int)'), onStateChanged)
#QObject.connect(http, SIGNAL('dataSendProgress (int,int)'), progress)
import sys
app = QApplication(sys.argv)
app.exec_()