diff --git a/scrapy/commands/fetch.py b/scrapy/commands/fetch.py index 3888da210..49fa18ab2 100644 --- a/scrapy/commands/fetch.py +++ b/scrapy/commands/fetch.py @@ -1,11 +1,11 @@ from __future__ import print_function +import sys, six from w3lib.url import is_url from scrapy.commands import ScrapyCommand from scrapy.http import Request from scrapy.exceptions import UsageError from scrapy.utils.spider import spidercls_for_request, DefaultSpider -from scrapy.utils.python import to_unicode class Command(ScrapyCommand): @@ -29,10 +29,10 @@ class Command(ScrapyCommand): help="print response HTTP headers instead of body") def _print_headers(self, headers, prefix): + prefix = prefix.encode() for key, values in headers.items(): for value in values: - print('%s %s: %s' % ( - prefix, to_unicode(key), to_unicode(value))) + self._print_bytes(prefix + b' ' + key + b': ' + value) def _print_response(self, response, opts): if opts.headers: @@ -40,7 +40,11 @@ class Command(ScrapyCommand): print('>') self._print_headers(response.headers, '<') else: - print(to_unicode(response.body)) + self._print_bytes(response.body) + + def _print_bytes(self, bytes_): + bytes_writer = sys.stdout if six.PY2 else sys.stdout.buffer + bytes_writer.write(bytes_ + b'\n') def run(self, args, opts): if len(args) != 1 or not is_url(args[0]):