# Response Response 对 requests 返回的response进行了封装,因此支持response所有方法 ## 功能点 ### 1. 智能解码 Response 对返回的文本进行了智能解码,可解决绝大多数乱码问题 ### 2. 智能转为绝对连接 若网页源码里的连接是相对连接,会自动转为绝对连接 ### 3. 支持xpath选择器 例如: 定位a标签连接,返回SelectorList ```python response.xpath("//a/@href") ``` 取第一个连接文本 ```python response.xpath("//a/@href").extract_first() ``` 取全部连接文本列表 ```python response.xpath("//a/@href").extract() ``` ### 4. 支持css选择器 例如: 定位a标签连接,返回SelectorList ```python response.css("a::attr(href)") ``` 取第一个连接文本 ```python response.css("a::attr(href)").extract_first() ``` 取全部连接文本列表 ```python response.css("a::attr(href)").extract() ``` ### 5. 支持正则 获取全部 ```python def re(self, regex, replace_entities=False): """ @summary: 正则匹配 --------- @param regex: 正则或者re.compile @param replace_entities: 为True时 去掉 等字符, 转义"为 " 等, 会使网页结构发生变化。如在网页源码中提取json, 建议设置成False --------- @result: 列表 """ ``` 获取第一个 ```python def re_first(self, regex, default=None, replace_entities=False): """ @summary: 正则匹配 --------- @param regex: 正则或者re.compile @param default: 未匹配到, 默认值 @param replace_entities: 为True时 去掉 等字符, 转义"为 " 等, 会使网页结构发生变化。如在网页源码中提取json, 建议设置成False --------- @result: 第一个值或默认值 """ ``` 例如获取全部连接: ``` response.re(">>content = b'\xe4\x3f\xa0\xe5\xa5\xbd' >>>str(content, errors='replace') '�?�好' >>>str(content, errors='strict') Traceback (most recent call last): File "/Users/Boris/workspace/feapder/venv2/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 1, in str(content, errors='strict') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 0: invalid continuation byte >>>str(content, errors='ignore') '?好' ``` 常规的response在解码时,使用了`replace`模式,这样会导致数据中可能混杂着乱码,我们不能及时发现. feapder.Response默认使用了`strict`默认,一旦某个字符解析失败,就会抛异常,防止乱码混入。然后通过人工指定编码,解决乱码问题。 若想修改feapder.Response的解码方式,可通过如下方式指定 ``` response.encoding_errors = "strict" # strict / replace / ignore ```