能抓取网页内容状态等。非常实用
import requests as res
# rs = res.get('http://www.lrmb.net')
rs = res.get('https://api.github.com/user', auth=('user', 'pass'))
rs.content.decode("unicode_escape")
# 获取状态
rs.status_code
print(rs.status_code)
# 获取headers
rs.headers['content-type']
print(rs.headers['content-type'])
# 获取编码
rs.encoding
print(rs.encoding)
# 获取内容
rs.text
print(rs.text)
# 获取json
rs.json()
print(rs.json())
如果报错,如下图:
File “E:\python\lib\site-packages equests\models.py”, line 910, in json
获取网址的这种类型的接口返回没有json返回,所以反作用域报错。换个网址就可以了
return complexjson.loads(self.text, **kwargs)
File “E:\python\lib\json__init__.py”, line 357, in loads
return _default_decoder.decode(s)
File “E:\python\lib\json\decoder.py”, line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “E:\python\lib\json\decoder.py”, line 355, in raw_decode
raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
推荐Request文档:
1.https://docs.python-requests.org/