MCPcopy
hub / github.com/scrapy/scrapy / from_body

Method from_body

scrapy/responsetypes.py:106–122  ·  view source on GitHub ↗

Try to guess the appropriate response based on the body content. This method is a bit magic and could be improved in the future, but it's not meant to be used except for special cases where response types cannot be guess using more straightforward methods.

(self, body: bytes)

Source from the content-addressed store, hash-verified

104 return Response
105
106 def from_body(self, body: bytes) -> type[Response]:
107 """Try to guess the appropriate response based on the body content.
108 This method is a bit magic and could be improved in the future, but
109 it's not meant to be used except for special cases where response types
110 cannot be guess using more straightforward methods."""
111 chunk = body[:5000]
112 chunk = to_bytes(chunk)
113 if not binary_is_text(chunk):
114 return self.from_mimetype("application/octet-stream")
115 lowercase_chunk = chunk.lower()
116 if b"<html>" in lowercase_chunk:
117 return self.from_mimetype("text/html")
118 if b"<?xml" in lowercase_chunk:
119 return self.from_mimetype("text/xml")
120 if b"<!doctype html>" in lowercase_chunk:
121 return self.from_mimetype("text/html")
122 return self.from_mimetype("text")
123
124 def from_args(
125 self,

Callers 2

from_argsMethod · 0.95
test_from_bodyMethod · 0.80

Calls 3

from_mimetypeMethod · 0.95
to_bytesFunction · 0.90
binary_is_textFunction · 0.90

Tested by 1

test_from_bodyMethod · 0.64