一、HTTP请求  

通过requests发送网络请求,方法有get post put delete head options

import requests
r=requests.get("http://github.com/timeline.json")
print(r.text)
r=requests.post("http://httpbin.org/post")
print(r.text)

二、传递URL参数

如果为url字符串传递数据,手工构建url那么数据就会以键值对的方式存在URL中,字典中的None不会被添加到URL的查询字符串里面

import requests
payload={"key1":"value1","key2":"value2","key3":("value2",)}
r=requests.get("http://httpbin.org/get",params=payload)
print(r.text) #获取相应内容
print(r.url) #http://httpbin.org/get?key1=value1&key2=value2&key3=value2

三、相应内容

1、自动编码的获取相应内容

r.text

2、二进制方式相应内容

r.content

3、获取传递的url

r.url

4、获取json响应内容

r.json()

注意:

a、如果json解压失败,会抛出一个异常

b、成功调用并不意味着响应成功,所以要用

r.raise_for_status()成功返回None或者r.status_code验证成功返回200

5、获取响应头

r.headers

由于响应头是字典形式,所以可以用get等字典方式获取内容

5、获取请求对象列表

r.history

四、定制请求

请求做成数据流文档

1、定制请求头,直接传递dict。给headers参数就可以

如:

import requests
url="https://api.github.con/some/endpoint"
headers={'user-agent':'my-app.0.0.1'}
r=requests.get(url,headers=headers)
print(r.text)

2、复杂的POST请求

a、要发送一些表单形式的数据传递参数data就可以,这个参数可以是元祖,列表

如:

import requests
url="https://api.github.con/some/endpoint"
headers={'user-agent':'my-app.0.0.1'}
r=requests.get(url,headers=headers)
print(r.text)

b、可以直接使用json参数传递

import requests
import json
payload={'some':'data'} r=requests.post("https://api.github.com/some/endpoint",json=payload)
print(r.text)

3、post多部分编码的文件,这里requests里面可以写入请求头

import requests
url="http://httpbin.org/post"
files={"file":open("a.txt","rb")} r=requests.post(url,files=files)
print(r.text)、

五、相应状态码

1、查询相应吗

r.status_code

2、抛出异常

r.raise_for_status()

六、cookie

1、获取cookie

import requests
url='http://www.baidu.com' r=requests.get(url)
print(r.cookies)

2、设置cookie

>>> url = 'http://httpbin.org/cookies'
>>> cookies = dict(cookies_are='working') >>> r = requests.get(url, cookies=cookies)
>>> r.text

3、超时

r=requests.get(url,timeout=1)

超时只对连接有效,如果在timeout时间没有应答,会引发异常

request模块 一基础部分的更多相关文章

  1. Python之request模块-基础用法

    Request模块参考中文手册:https://requests.readthedocs.io/zh_CN/latest/ Request模块 1.查看pip已装包(模块)的安装信息(模块的路径.版本 ...

  2. request 模块详细介绍

    request 模块详细介绍 request Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装 ...

  3. node.js的http模块的基础 学到的东西

    node.js的http模块的基础 学到的东西 其中客户端:我们在node.js中如果要请求服务端中的js或者其他脚本的话要使用http.request()方法他会返回http.ClientReque ...

  4. python3中urllib库的request模块详解

    刚刚接触爬虫,基础的东西得时时回顾才行,这么全面的帖子无论如何也得厚着脸皮转过来啊! 原帖地址:https://www.2cto.com/kf/201801/714859.html 什么是 Urlli ...

  5. dojo/request模块整体架构解析

    总体说明 做前端当然少不了ajax的使用,使用dojo的童鞋都知道dojo是基于模块化管理的前端框架,其中对ajax的处理位于dojo/request模块.一般情况下我们使用ajax请求只需要引入do ...

  6. node.js的request模块

    request模块让http请求变的更加简单.最简单的一个示例: 1: var request = require('request'); 2:  3: request('http://www.goo ...

  7. es 模块的基础知识,深度了解

    // 一模块的基础知识 /** * export :用于模块输出的出口 * import :文件引入的入口 */ // 1,第一种方式使用export方式输出 var a = 'a'; var b = ...

  8. python接口自动化测试(一)-request模块

    urllib.request模块是python3针对处理url的. 1. 首先导入: from urllib import request 2. 构造url,构造url的headers信息和传参[re ...

  9. 第5月第10天 node.js的request模块

    1.node.js的request模块 http://www.cnblogs.com/meteoric_cry/archive/2012/08/18/2645530.html

随机推荐

  1. ASP.NET动态网站制作(24)-- ADO.NET(3)

    前言:ADO.NET的第三节课.今天主要讲SQL Helper. 内容: 1.DataReader和DataSet的区别: (1)DataReader是一行一行地读,且只能向前读:DataSet是一次 ...

  2. 更新mac系统和更新到Xcode7.3版本出现的: cannot create __weak reference in file using manual reference counting

    之前的编程没有遇到过,应该是苹果官方那边又做了新规吧. 不过不要紧,只要根据这个就能解决报错问题.  Set Build Settings -> Apple LLVM 7.1 - Languag ...

  3. IOS开发之----常用加密方法

    本文转载至 http://blog.csdn.net/wildfireli/article/details/23191983 (AES.MD5.Base64) 分类: iPhone 2014-04-0 ...

  4. 47、ListView setSelection() 和 setSelectionFromTop()

    http://blog.csdn.net/manoel/article/details/39183025 http://blog.csdn.net/a859522265/article/details ...

  5. openssl 升级操作 -2

    首先我觉得没事就用绿盟扫漏洞的公司,就是闲的蛋疼,傻逼!不少服务器使用nginx,如果openssl 是静态编译的,直接将openssl 编译到nginx里面去了,这就意味着,单纯升级openssl ...

  6. hdu5646(数学)

    小学数学,脑补 一开始看到这题,猜了个规律想写但是我是拒绝的. 因为我无法证明. 好吧,主要还是小学数学没学好吧. 要理解这题,首先得搞懂一个重要问题.假设C=A+B,怎样选择两个正整数使得A*B最大 ...

  7. hdu 5471(状压DP or 容斥)

    想了最复杂的思路,用了最纠结的方法,花了最长的时间,蒙了一种规律然后莫名其妙的过了. MD 我也太淼了. 后面想了下用状压好像还是挺好写的,而且复杂度也不高.推出的这个容斥的规律也没完全想透我就CAO ...

  8. Reverse and Compare(DP)

    Reverse and Compare Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement Yo ...

  9. Machine Learning - week 2 - 编程练习

      3. % J = COMPUTECOST(X, y, theta) computes the cost of using theta as the % parameter for linear r ...

  10. Thinking In Myself

    what is the I want to be? you know yourself?  what is your dream? your interest? your passion? why y ...