import requests
import json # 1、HTTP方法
requests.get('https://github.com/timeline.json') #GET请求
requests.post('http://httpbin.org/post') #POST请求
requests.put('http://httpbin.org/put') #PUT请求
requests.delete('http://httpbin.org/delete') #DELETE请求
requests.head('http://httpbin.org/get') #HEAD请求
requests.options('http://httpbin.org/get') #OPTIONS请求 # 2、Make a Request
url = 'http://www.baidu.com'
req = requests.get(url)
print(req.text) # 3、response属性
import requests
response = requests.get('http://www.baidu.com/s', params={'wd': 'python'}) # GET参数实例
print(response.url)
print(response.text) #返回的内容,字符串形式
print(response.status_code) #返回码
print(response.content) #返回的内容,二进制形式
print(response.headers)
print(response.headers['content-type'])
print(response.headers.get('content-type'))
print(response.encoding)
print(response.json())
response.raise_for_status() #抛出异常 非200响应 # 4、get带参数
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('http://httpbin.org/get', params=payload)
print(r.url)
print(r.text) # 5、数据流方式读取结果
r = requests.get('https://api.github.com/events', stream=True)
print(r.raw) #不解码
print(r.raw.read(10))
with open('output.txt', 'wb') as fd:
for chunk in r.iter_content(chunk_size=128): #解码
fd.write(chunk) # 6、自定义头信息
url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'} # 字符串,字节串,Unicode
r = requests.get(url, headers=headers) # 7、post数据:字典
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
print(r.text) # 8、post数据:列表
payload = (('key1', 'value1'), ('key1', 'value2'))
r = requests.post('http://httpbin.org/post', data=payload)
print(r.text) # 9、post数据:字符串
url = 'http://httpbin.org/post'
payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload))
print(r.text) # 10、post数据:字符串
url = 'http://httpbin.org/post'
payload = {'some': 'data'}
r = requests.post(url, json=payload)
print(r.text) # 11、上传多部分编码的文件
url = 'http://httpbin.org/post'
files = {'file': open('a.xls', 'rb')}
r = requests.post(url, files=files)
print(r.text) # 12、上传多部分编码的文件
url = 'http://httpbin.org/post'
files = {'file': ('a.xls', open('a.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': ''})}
r = requests.post(url, files=files)
print(r.text) # 13、字符串当文件发
url = 'http://httpbin.org/post'
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}
r = requests.post(url, files=files)
print(r.text) # 14、cookies
url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)
print(r.text) # 15、cookies
jar = requests.cookies.RequestsCookieJar()
jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
url = 'http://httpbin.org/cookies'
r = requests.get(url, cookies=jar)
print(r.text) # 16、timeout
requests.get('http://github.com', timeout=0.001)

python3 requests模块 基本操作的更多相关文章

  1. python3: requests模块的使用;

    requests库常用于http请求,可以很方便对网页进行爬取: 主要方法(七个): 方法 解释 requests.request() 构造一个请求,支持以下各种方法 requests.get() 获 ...

  2. python3 requests模块

    一.Requests用法: 1.发送请求: 1).请求类型:req_obj = requests.get("https://www.baidu.com")requests支持多种请 ...

  3. python3:requests模块-写了一点

    使用requests,它的七个主要方法,在这里只讲两个:get.post >>> import requests >>> r=requests.get(" ...

  4. python3 requests 模块 json参数和data参数区别

    json 表示使用application/json方式提交请求 data 使用application/form-urlencode方式提交请求

  5. [实战演练]python3使用requests模块爬取页面内容

    本文摘要: 1.安装pip 2.安装requests模块 3.安装beautifulsoup4 4.requests模块浅析 + 发送请求 + 传递URL参数 + 响应内容 + 获取网页编码 + 获取 ...

  6. python3使用requests模块完成get/post/代理/自定义header/自定义Cookie

    一.背景说明 http请求的难易对一门语言来说是很重要的而且是越来越重要,但对于python一是urllib一些写法不太符合人的思维习惯文档也相当难看,二是在python2.x和python3.x中写 ...

  7. Python3:Requests模块的异常值处理

    Python3:Requests模块的异常值处理 用Python的requests模块进行爬虫时,一个简单高效的模块就是requests模块,利用get()或者post()函数,发送请求. 但是在真正 ...

  8. (转)Python3之requests模块

    原文:https://www.cnblogs.com/wang-yc/p/5623711.html Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另 ...

  9. Python3之requests模块

    Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务. 发送G ...

随机推荐

  1. 提高sqlite 的运行性能(转载)

    原文地址: https://blog.devart.com/increasing-sqlite-performance.html One the major issues a developer en ...

  2. kubernetes-notes--阅读笔记

    文档地址 https://www.huweihuang.com/kubernetes-notes/

  3. Struts2页面遍历

    <s:iterator />可以遍历 数据栈里面的任何数组,集合等等 在使用这个标签的时候有三个属性值得我们关注      1. value属性:可选的属性,value属性是指一个被迭代的 ...

  4. Protocol_ISIS

    集成ISIS协议 作者:Danbo 2015-7-5 ISIS的意思是表示中间系统,并且是为OSI无连接网络协议(OSI Conectionless Network Protocol,CLNP)设计的 ...

  5. Android Weekly Notes Issue #246

    Android Weekly Issue #246 February 26th, 2017 Android Weekly Issue #246 本期内容包括: RecyclerView上的Shared ...

  6. Windows程序设计(1)——Win32运行原理(二)

    创建进程 1 进程和线程 2 应用程序的启动过程 3 CreateProcess函数 4 实例 3 创建进程 3.1 进程和线程 进程通常被定义为一个存在运行的程序的实例.进程是一个正在运行的程序,它 ...

  7. windows server 2008 + IIS 7.5实现多用户FTP(多账号对应不同目录

    在windows server 2003 + IIS 6 的时候,就已经能实现多用户FTP的功能,不过设置有写繁琐,如果站点多的话,设置账号.权限这些东西都要搞很久.Windows server 20 ...

  8. RTree算法Java实现 JSI RTree Library的调用实例 标签:jsi-rtree-library

    1. [代码]jsi-rtree-library /** *  */package com.mycompany.project; //package net.sourceforge.jsi.examp ...

  9. ios 使用json

    1.从https://github.com/stig/json-framework/中下载json框架:json-framework 2.解压下载的包,将class文件夹下的所有文件导入到当前工程下. ...

  10. codeforces 463B Caisa and Pylons 解题报告

    题目链接:http://codeforces.com/problemset/problem/463/B 题目意思:Caisa 站在 0 pylon 并且只有 0 energy,他需要依次跳过1 pyl ...