Requests API

http://docs.python-requests.org/zh_CN/latest/api.html

发送请求requerts

参数类型描述
methodstr‘get’/'post’等
urlstr请求的网页,注意post的时候提交数据对应的页面是form的action
paramsdicturl里面的参数
datadictpost方法的时候提交的dict,在body里
json
headersdict表头
cookiesdict or CookieJar可以通过第一次的response来获得
filesdictvalue对应的是file类型的文件,既open()函数返回的对象
auth
timeoutfloat等待返回的时间,超过就放弃请求
proxiesdict代理ip设置
streamboolean是否以流的形式返回,默认false
verifyboolean是否验证安全证书,如果遇到验证不通过直接写false

params参数可以用字典的形式传入,也可以在url里直接用"?"来连接参数

poxies格式,根据不同的协议选择不同的ip

proxies = {
  "http": "http://12.34.56.78:9000",
  "https": "http://12.34.56.78:9000",
}

发送请求的7中方法

  1. requests.request(method, url, **kwargs)
  2. requests.head(url, **kwargs)
  3. requests.get(url, params=None, **kwargs)
  4. requests.put(url, data=None, **kwargs)
  5. requests.patch(url, data=None, **kwargs)
  6. requests.delete(url, **kwargs)

get常用方法

requests.get(url, params=None, **kwargs)发送一个get请求

参数描述
url网页地址
params网页参数
headers表头参数
timeout等待时间
proxiesIP代理
stream二进制格式返回

返回类型:requests.Response

请求对话

class requests.Session

属性

属性描述
self.headers
self.auth
self.proxies
self.hooks
self.params = {}
self.stream = False
self.verify = True
self.cert = None
self.max_redirects
self.trust_env
self.cookies
self.adapters
self.redirect_cache

对象方法

方法描述
def prepare_request(self, request)
def request()
def get(self, url, **kwargs)
def options(self, url, **kwargs)
def head(self, url, **kwargs)
def post(self, url, data=None, json=None, **kwargs)
def put(self, url, data=None, **kwargs)
def patch(self, url, data=None, **kwargs)
def delete(self, url, **kwargs)
def send(self, request, **kwargs)
def merge_environment_settings(self, url, proxies, stream, verify, cert)
def get_adapter(self, url)
def close(self)
def mount(self, prefix, adapter)

查看响应内容Response对象

对象属性

属性描述
.status_codeHTTP响应状态,比如404或200(int)
.headers获得响应头(dict)
.raw返回原始数据,需要设置stream=True(file-like)
.url最后的连接(str)
.encoding设置编码格式(str)
.history获得重定向连接(list)
.reason响应状态的翻译(str)
.cookies获得cookie(CookieJar)
.elapsed获得响应时间
.request返回PreparedRequest对象
.ok是否连接成功(bool)
.is_redirect是否重定向(bool)
.is_permanent_redirect
.apparent_encoding返回编码(str)
.content返回二进制(str)
.text返回文本(str)
.links返回网址(list)

对象方法

方法描述
ok(self)是否连接成功(bool)
is_redirect(self)是否重定向(bool)
is_permanent_redirect(self)
apparent_encoding(self)返回编码(str)
iter_content(self, chunk_size=1, decode_unicode=False)二进制迭代
iter_lines(self, chunk_size=ITER_CHUNK_SIZE, decode_unicode=None, delimiter=None)内容迭代
content(self)返回二进制(str)
text(self)返回文本(str)
json(self, **kwargs)返回json
links(self)返回网址(list)
raise_for_status(self)返回错误信息
close(self)释放连接

cookie

class requests.cookies.RequestsCookieJar(policy=None)
方法描述
get_dict(domain=None, path=None)返回cookie对应的字典
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值