首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

twisted-webclient将网页另存为资料

2012-10-07 
twisted-webclient将网页另存为文件from twisted.web import clientimport tempfiledef downloadToTempFil

twisted-webclient将网页另存为文件

from twisted.web import clientimport tempfiledef downloadToTempFile(url): """ Given a URL, returns a Deferred that will be called back with the name of a temporary file containing the downloaded data. """ tmpfd, tempfilename = tempfile.mkstemp( ) os.close(tmpfd) return client.downloadPage(url, tempfilename).addCallback( returnFilename, tempfilename)def returnFilename(result, filename): return filenameif __name__ == "_ _main_ _": import sys, os from twisted.internet import reactor def printFile(filename): for line in file(filename, 'r+b'): sys.stdout.write(line) os.unlink(filename) # delete file once we're done with it reactor.stop( ) def printError(failure): print >> sys.stderr, "Error:", failure.getErrorMessage( ) reactor.stop( ) if len(sys.argv) == 3: url = sys.argv[2] downloadToTempFile(url).addCallback( printFile).addErrback( printError) reactor.run( ) else: print "Usage: %s <URL>" % sys.argv[0]

?

热点排行