A/C python 请求文档,with 语句可以与请求一起使用以提高速度。
with requests.get(' http://httpbin.org/get ', stream=True) as r: # 在这里处理响应。
那么为什么这会返回“属性错误”呢?
Traceback (most recent call last):
File "<pyshell#101>", line 1, in <module>
with requests.post(url,headers=headers,data=data,stream=True) as post_res:
AttributeError: __exit__
Run Code Online (Sandbox Code Playgroud)
代码:
with requests.post(url,headers=headers,data=data,stream=True) as post_res:
print(b'Name' in post_res.content)
Run Code Online (Sandbox Code Playgroud)
PS 这在没有“with”语句的情况下工作正常。