From 5b9a7814a5861a0cb0b2359cd8ebab6cf144e57a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gra=C3=B1a?= Date: Wed, 25 Apr 2012 16:04:37 -0300 Subject: [PATCH] support TextResponse in open_in_browser util --- scrapy/utils/response.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/scrapy/utils/response.py b/scrapy/utils/response.py index e3098a300..85d0b60f0 100644 --- a/scrapy/utils/response.py +++ b/scrapy/utils/response.py @@ -13,7 +13,7 @@ from twisted.web import http from twisted.web.http import RESPONSES from w3lib import html -from scrapy.http import Response, HtmlResponse +from scrapy.http import Response, HtmlResponse, TextResponse def body_or_str(obj, unicode=True): assert isinstance(obj, (Response, basestring)), \ @@ -76,13 +76,17 @@ def open_in_browser(response, _openfunc=webbrowser.open): tag for external links to work """ # XXX: this implementation is a bit dirty and could be improved - if not isinstance(response, HtmlResponse): + body = response.body + if isinstance(response, HtmlResponse): + if '', '' % response.url) + ext = '.html' + elif isinstance(response, TextResponse): + ext = '.txt' + else: raise TypeError("Unsupported response type: %s" % \ response.__class__.__name__) - body = response.body - if '', '' % response.url) - fd, fname = tempfile.mkstemp('.html') + fd, fname = tempfile.mkstemp(ext) os.write(fd, body) os.close(fd) return _openfunc("file://%s" % fname)