requests的基本用法

r = requests.get(‘https://api.github.com/events‘, params = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘})
r = requests.get(‘https://api.github.com/some/endpoint‘, headers={‘user-agent‘: ‘my-app/0.0.1‘})
r = requests.get(‘http://httpbin.org/cookies‘, cookies=dict(cookies_are=‘working‘))
r = requests.get(‘http://github.com‘, allow_redirects=False) #禁用重定向
r = requests.get(‘http://github.com‘, timeout=0.001)  # 设置请求超时时间
r = requests.get(url, cookies=requests.cookies.RequestsCookieJar().set(‘tasty_cookie‘, ‘yum‘,
                                                                        domain=‘httpbin.org‘,
                                                                        path=‘/cookies‘))
r = requests.post(‘http://httpbin.org/post‘, data = {‘key‘:‘value‘})
r = requests.post(‘http://httpbin.org/post‘, data=((‘key1‘, ‘value1‘), (‘key1‘, ‘value2‘))) #多个元素同一个key
r = requests.post(‘https://api.github.com/some/endpoint‘, data=json.dumps({‘some‘: ‘data‘}))
r = requests.post(‘https://api.github.com/some/endpoint‘, json={‘some‘: ‘data‘})
r = requests.post(‘http://httpbin.org/post‘, files={‘file‘: open(‘report.xls‘, ‘rb‘)})  # 上传文件
r = requests.post(url, files={‘file‘: (‘report.xls‘,
                                    open(‘report.xls‘, ‘rb‘),
                                    ‘application/vnd.ms-excel‘,
                                    {‘Expires‘: ‘0‘})})  # 显式地设置文件名,文件类型和请求头
r = requests.put(‘http://httpbin.org/put‘, data = {‘key‘:‘value‘})
r = requests.delete(‘http://httpbin.org/delete‘)
r = requests.head(‘http://httpbin.org/get‘)
r = requests.options(‘http://httpbin.org/get‘)

r.status_code: 返回的状态码
r.url: 打印输出拼接后的URL
r.text:响应的body内容
r.encoding: 改变响应body的编码方式
r.content: 二进制的响应body。content会自动解码 gzip 和deflate压缩
r.headers: 以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回None
r.json(): 响应的json内容
r.raw(): 原始套接字响应
r.cookies[‘example_cookie_name‘]: 访问响应中的cookies
r.history: requests默认自动重定向,可以看重定向的记录

requests高级用法:http://docs.python-requests.org/zh_CN/latest/user/advanced.html#advanced

原文地址:https://www.cnblogs.com/staff/p/10002904.html

时间: 2024-07-31 14:19:39

requests的基本用法的相关文章

Python3网络爬虫实战-25、requests:高级用法

在前面一节我们了解了 Requests 的基本用法,如基本的 GET.POST 请求以及 Response 对象的用法,本节我们再来了解下 Requests 的一些高级用法,如文件上传,代理设置,Cookies 设置等等. 1. 文件上传 我们知道 Reqeuests 可以模拟提交一些数据,假如有的网站需要我们上传文件,我们同样可以利用它来上传,实现非常简单,实例如下: import requests files = {'file': open('favicon.ico', 'rb')} r =

python爬虫---requests库的用法

requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多 因为是第三方库,所以使用前需要cmd安装 pip install requests 安装完成后import一下,正常则说明可以开始使用了. 基本用法: requests.get()用于请求目标网站,类型是一个HTTPresponse类型 import requests response = requests.get('http://www.baidu.com')print(response.status_c

爬虫 requests,bs4 用法示例

requests 模块 用法 import requests # 1. 方法""" requests.get requests.post requests.put requests.delete ... requests.request(method='POST') """ # 2. 参数 """ 2.1 url 2.2 headers 2.3 cookies 2.4 params 2.5 data,传请求体 req

requests模块简单用法

1 import requests 2 import random 3 4 # 请求发送的网址url 5 url = 'https://www.baidu.com' 6 # 请求头信息,通常用于伪装浏览器,通过服务器校验 7 headers = { 8 9 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safa

requests库的用法

import requests headers = {"headers头部文件"} url = 'http://www.baidu.com/' html = requests.get(url,headers=headers) html.encoding = 'gbk' #转成该网站的格式 print(html.text) 详细用法请参见 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓ https://www.cnblogs.com/mzc1997/p/7813801.html 原文地址:https://

Python教程 | Requests的基本用法

下面我就给大家整理了Requests库的使用方法和细节. 什么是Requests Requests是Python语言编写,基于urllib3,采用Apache2 Licensed开源协议的HTTP库.它比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求.是Python实现的简单易用的HTTP库. 安装也很简单: pip install requests Request的语法操作 1.实例引入 2.各种请求方式 请求 1.基本GET请求 2.带参数的GET请求这个我们前面有使

requests 用法

# 百度import requests url = 'http://www.baidu.com/' # requests 的 get用法reponse = requests.get(url) with open('baidu23.html', 'wb') as f: f.write(reponse.content) # 百度翻译import requestsimport json url = 'http://fanyi.baidu.com/sug' def translate(kw): form

Python爬虫学习一之Requests包使用方法

Requests函数库是学习Python爬虫必备之一, 能够帮助我们方便地爬取. 本文主要参考了其官方文档. Requests安装: requests目前的版本是v2.11.1, 在WINDOWS上可以通过命令行窗口(运行cmd命令), 利用pip进行自动地安装(很方便): > pip install requestsCollecting requests Downloading requests-2.11.1-py2.py3-none-any.whl <514kB>Installing

python3爬虫初探(二)之requests

关于请求网页,不得不提requests这个库,这是爬虫经常用到的一个第三方库,用pip安装即可. requests用法很多,这里只写一些基础的,其他高级功能可参考官方文档. import requests url = 'http://www.baidu.com' #这里用get方法用来请求网页,其他还有post等方法来请求网页 data = requests.get(url) print(data) #<Response [200]> print(data.text)#这里的 .text 就等