diff --git a/docs/topics/request-response.rst b/docs/topics/request-response.rst index ea64d1599..2e92961a9 100644 --- a/docs/topics/request-response.rst +++ b/docs/topics/request-response.rst @@ -445,10 +445,10 @@ Response objects .. attribute:: Response.body - A str containing the body of this Response. Keep in mind that Response.body - is always a str. If you want the unicode version use - :meth:`TextResponse.body_as_unicode` (only available in - :class:`TextResponse` and subclasses). + The body of this Response. Keep in mind that Response.body + is always a bytes object. If you want the unicode version use + :attr:`TextResponse.txt` (only available in :class:`TextResponse` + and subclasses). This attribute is read-only. To change the body of a Response use :meth:`replace`. @@ -542,6 +542,21 @@ TextResponse objects :class:`TextResponse` objects support the following attributes in addition to the standard :class:`Response` ones: + .. attribute:: TextResponse.text + + Response body, as unicode. + + The same as ``response.body.decode(response.encoding)``, but the + result is cached after the first call, so you can access + ``response.text`` multiple times without extra overhead. + + .. note:: + + ``unicode(response.body)`` is not a correct way to convert response + body to unicode: you would be using the system default encoding + (typically `ascii`) instead of the response encoding. + + .. attribute:: TextResponse.encoding A string with the encoding of this response. The encoding is resolved by @@ -568,20 +583,6 @@ TextResponse objects :class:`TextResponse` objects support the following methods in addition to the standard :class:`Response` ones: - .. method:: TextResponse.body_as_unicode() - - Returns the body of the response as unicode. This is equivalent to:: - - response.body.decode(response.encoding) - - But **not** equivalent to:: - - unicode(response.body) - - Since, in the latter case, you would be using the system default encoding - (typically `ascii`) to convert the body to unicode, instead of the response - encoding. - .. method:: TextResponse.xpath(query) A shortcut to ``TextResponse.selector.xpath(query)``:: @@ -594,6 +595,11 @@ TextResponse objects response.css('p') + .. method:: TextResponse.body_as_unicode() + + The same as :attr:`text`, but available as a method. This method is + kept for backwards compatibility; please prefer ``response.text``. + HtmlResponse objects -------------------- diff --git a/scrapy/downloadermiddlewares/ajaxcrawl.py b/scrapy/downloadermiddlewares/ajaxcrawl.py index 6b543b823..da373eca2 100644 --- a/scrapy/downloadermiddlewares/ajaxcrawl.py +++ b/scrapy/downloadermiddlewares/ajaxcrawl.py @@ -63,7 +63,7 @@ class AjaxCrawlMiddleware(object): Return True if a page without hash fragment could be "AJAX crawlable" according to https://developers.google.com/webmasters/ajax-crawling/docs/getting-started. """ - body = response.body_as_unicode()[:self.lookup_bytes] + body = response.text[:self.lookup_bytes] return _has_ajaxcrawlable_meta(body) diff --git a/scrapy/downloadermiddlewares/robotstxt.py b/scrapy/downloadermiddlewares/robotstxt.py index c061c2407..d4a33dc36 100644 --- a/scrapy/downloadermiddlewares/robotstxt.py +++ b/scrapy/downloadermiddlewares/robotstxt.py @@ -83,8 +83,8 @@ class RobotsTxtMiddleware(object): def _parse_robots(self, response, netloc): rp = robotparser.RobotFileParser(response.url) body = '' - if hasattr(response, 'body_as_unicode'): - body = response.body_as_unicode() + if hasattr(response, 'text'): + body = response.text else: # last effort try try: body = response.body.decode('utf-8') diff --git a/scrapy/http/request/form.py b/scrapy/http/request/form.py index 5501634d3..2862dc096 100644 --- a/scrapy/http/request/form.py +++ b/scrapy/http/request/form.py @@ -64,8 +64,8 @@ def _urlencode(seq, enc): def _get_form(response, formname, formid, formnumber, formxpath): """Find the form element """ - text = response.body_as_unicode() - root = create_root_node(text, lxml.html.HTMLParser, base_url=get_base_url(response)) + root = create_root_node(response.text, lxml.html.HTMLParser, + base_url=get_base_url(response)) forms = root.xpath('//form') if not forms: raise ValueError("No