未下载Python QtWebKit第2个链接
我正在尝试下载两个链接的内容。第一个链接工作正常,内容作为html文件下载。但第二个链接(文本文件)的内容未下载。请帮助我 这是我的密码:未下载Python QtWebKit第2个链接,qt,qt4,pyqt4,pyside,qtwebkit,Qt,Qt4,Pyqt4,Pyside,Qtwebkit,我正在尝试下载两个链接的内容。第一个链接工作正常,内容作为html文件下载。但第二个链接(文本文件)的内容未下载。请帮助我 这是我的密码: from PySide.QtCore import * from PySide.QtGui import * from PySide.QtWebKit import * import sys import codecs class Downloader(QObject): # To be emitted when every items are d
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
import sys
import codecs
class Downloader(QObject):
# To be emitted when every items are downloaded
done = Signal()
def __init__(self, urlList, parent = None):
super(Downloader, self).__init__(parent)
self.urlList = urlList
self.counter = 0
# As you probably don't need to display the page
# you can use QWebPage instead of QWebView
self.page = QWebPage(self)
self.page.loadFinished.connect(self.save)
self.startNext()
def currentUrl(self):
return self.urlList[self.counter][0]
def currentFilename(self):
return self.urlList[self.counter][1]
def startNext(self):
print "Downloading %s..."%self.currentUrl()
self.page.mainFrame().load(self.currentUrl())
def save(self, ok):
if ok:
data = self.page.mainFrame().toHtml()
with codecs.open(self.currentFilename(), encoding="utf-8", mode="w") as f:
f.write(data)
print "Saving %s to %s."%(self.currentUrl(), self.currentFilename())
else:
print "Error while downloading %s\nSkipping."%self.currentUrl()
self.counter += 1
if self.counter < len(self.urlList):
self.startNext()
else:
self.done.emit()
urlList = [("http://www.nsf.gov/awardsearch/simpleSearchResult?queryText=8","nsf.html"),
("http://www.nsf.gov/awardsearch/ExportResultServlet?exportType=txt","a.txt")]
app = QApplication(sys.argv)
downloader = Downloader(urlList)
# Quit when done
downloader.done.connect(app.quit)
# To view the pages
web = QWebView()
# To prevent user action that would interrupt the current page loading
web.setDisabled(True)
web.setPage(downloader.page)
web.show()
sys.exit(app.exec_())
从PySide.QtCore导入*
从PySide.QtGui导入*
从PySide.QtWebKit导入*
导入系统
导入编解码器
类下载程序(QObject):
#在下载每个项目时发出
完成=信号()
def uuu init uuuu(self,urlist,parent=None):
超级(下载程序,自我)。\uuuuu初始化\uuuuuuu(父级)
self.urlist=urlist
self.counter=0
#因为您可能不需要显示页面
#您可以使用QWebPage而不是QWebView
self.page=QWebPage(self)
self.page.loadFinished.connect(self.save)
self.startNext()
def currentUrl(自):
返回self.urlist[self.counter][0]
def currentFilename(自):
返回self.urlist[self.counter][1]
def startNext(自我):
打印“正在下载%s..%self.currentUrl()
self.page.mainFrame().load(self.currentUrl())
def保存(自我,正常):
如果确定:
data=self.page.mainFrame().toHtml()
使用codecs.open(self.currentFilename(),encoding=“utf-8”,mode=“w”)作为f:
f、 写入(数据)
打印“将%s保存到%s”。%(self.currentUrl(),self.currentFilename())
其他:
打印“下载%s时出错\n跳过。”%self.currentUrl()
self.counter+=1
如果self.counter
我怀疑存在webkit浏览器不知道如何处理.txt文件的问题。我正在处理一个类似的问题(尽管我想下载一个.xls)——PyQT webkit浏览器似乎忽略了非标准网站的文件类型。