基本实例

import  requests

url= 'https://www.baidu.com/'
response = requests.get(url)
print(type(response))
print(response.status_code)#状态码
print(type(response.text))
print(response.text)#打开网页源代码
print(response.cookies)#获取cookies

各种请求方式

import  requests

url= 'https://www.baidu.com/'
requests.get(url)
requests.put(url)
requests.delete(url)
requests.head(url)
requests.options(url)

带参数的GET请求

import  requests

data={

}
reponse = requests.get(url,params=data)

解析JSON

import  requests
import json reponse = requests.get(url)
print(requests.json())
print(json.loads(reponse.text))

获取二进制数据和保存

import  requests
import json reponse = requests.get(url)
print(reponse.text)
print(reponse.content)
import  requests
import json reponse = requests.get(url)
with open(' ',' ') as f:
f.write(reponse.content)
f.close()

添加headers

import  requests
import json headers = { }
response = requests.get(url,headers=headers)

基本POST请求

mport requests
import json data = { }
headers={ }
response = requests.post(url,data=data,headers=headers)

Reponse属性

import  requests

url= 'https://www.baidu.com/'
response = requests.get(url)
print(type(response))
print(response.status_code)#状态码
print(type(response.text))
print(response.text)#打开网页源代码
print(response.cookies)#获取cookies
print(response.history)
print(response.url)

文件上传

import  requests

files = {'file':open('','rb')}
reponse = requests.post(url,files=files)

维持会话

import  requests

s = requests.session()
s.get(url_1)
response = s.get(url_2)

证书认证

import  requests
from requests.packages import urllib3
urllib3.disable_warnings()#消除警告
response = requests.get(url,verify=False)

代理

import  requests
proxies = {
"http":
"https":
}
requests.get(url,proxies=proxies)

pip3 install 'requests[socks]' 使用socks代理

import  requests
from requests.exceptions import ReadTimeout try:
response = requests.get(url,timeout= )
except ReadTimeout:
print("time out")

认证设置

import  requests
from requests.auth import HTTPBasicAuth response = requests.get(url,auth=HTTPBasicAuth('',''))
import  requests
response = requests.get(url,auth=('',''))

异常处理

Request库基本使用的更多相关文章

  1. Python3 urllib.request库的基本使用

    Python3 urllib.request库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地. 在Python中有很多库可以用来抓取网页,我们先学习urlli ...

  2. Python request库与爬虫框架

    Requests库的7个主要方法  requests.request():构造一个请求,支持以下各方法的基础方法  requests.get():获取HTML网页的主要方法,对应于HTTP的GET  ...

  3. Request库使用response.text返回乱码问题

    我们日常使用Request库获取response.text,这种调用方式返回的text通常会有乱码显示: import requests res = requests.get("https: ...

  4. 爬虫——urllib.request库的基本使用

    所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地.在Python中有很多库可以用来抓取网页,我们先学习urllib.request.(在python2.x中为urllib2 ...

  5. 爬虫request库规则与实例

    Request库的7个主要方法: requests.request(method,url,**kwargs) ​ method:请求方式,对应get/put/post等7种: ​ r = reques ...

  6. Python网络爬虫与信息提取[request库的应用](单元一)

    ---恢复内容开始--- 注:学习中国大学mooc 嵩天课程 的学习笔记 request的七个主要方法 request.request() 构造一个请求用以支撑其他基本方法 request.get(u ...

  7. Request库的安装与使用

    Request库的安装与使用 安装 pip install reqeusts Requests库的7个主要使用方法 requests.request() 构造一个请求,支撑以下各方法的基础方法 req ...

  8. python网络爬虫学习笔记(一)Request库

    一.Requests库的基本说明 引入Rquests库的代码如下 import requests 库中支持REQUEST, GET, HEAD, POST, PUT, PATCH, DELETE共7个 ...

  9. Request库学习

    0x00前言 这库让我爱上了python  碉堡! 开心去学了一些python,然后就来学这个时候神库~~ 资料来源:http://cn.python-requests.org/en/latest/u ...

  10. 爬虫入门【1】urllib.request库用法简介

    urlopen方法 打开指定的URL urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, ca ...

随机推荐

  1. 用sass的minix定义一些代码片段,且可传参数

    /** *@module功能 *@description生成全屏方法 *@method fullscreen *@version 1.7.0 *@param{Integer}$z-index 指定层叠 ...

  2. 装饰器,装饰器多参数的使用(*arg, **kwargs),装饰器的调用顺序

    一.#1.执行outer函数,并且将其下面的函数名,当作参数 #2.将outer的返回值重新赋值给f1 = outer的返回值 #3.新f1 = inner #4.func = 原f1 #!/usr/ ...

  3. cocos2d-js 定时器

    1.scheduleUpdate 节点中有scheduleUpdate接口,通过这个接口,可以让游戏在每帧执行都执行update方法 var ScheduleUpdateLayer = cc.Laye ...

  4. c++的单例模式及c++11对单例模式的优化

    单例模式 单例模式,可以说设计模式中最常应用的一种模式了,据说也是面试官最喜欢的题目.但是如果没有学过设计模式的人,可能不会想到要去应用单例模式,面对单例模式适用的情况,可能会优先考虑使用全局或者静态 ...

  5. SQL IN, NOT IN, EXISTS, NOT EXISTS

    IN与EXISTS执行流程 IN:在查询的时候,首先查询子查询的表,然后将内表和外表做一个笛卡尔积,然后按照条件进行筛选.所以相对内表比较小的时候,in的速度较快.(IN时不对NULL进行处理) EX ...

  6. ROS Learning-001 安装 ROS indigo

    如何在 Ubuntu14.04 上安装 ROS indigo 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LTS ROS 版本 ...

  7. PCL—关键点检测(rangeImage)低层次点云处理

    博客转载自:http://www.cnblogs.com/ironstark/p/5046479.html 关键点又称为感兴趣的点,是低层次视觉通往高层次视觉的捷径,抑或是高层次感知对低层次处理手段的 ...

  8. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-006BitonicMax

    package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; /*** ...

  9. 解决PendingIntent传递参数为空的问题

    PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,  0); 在接收端,接收的数据一直为null,在google ...

  10. Java50道经典习题-程序36 移动位置

    题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数 分析:比如有4个数n=4; 1 2 3 4 各个数向后移动2位m=2,变为 1 2 3 4 将最后m个数,即将最后2个数 ...