基本实例

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. Decorator模式 装饰器模式

    Android 使用了装饰器模式 1. 概述 若你从事过面向对象开发,实现给一个类或对象增加行为,使用继承机制,这是所有面向对象语言的一个基本特性.如果已经存在的一个类缺少某些方法,或者须要给方法添加 ...

  2. Oracle pl/sql 显示游标和隐式游标

    显示游标 一.定义语法:        CURSOR <游标名> IS         <SELECT 语句>         [FOR UPDATE | FOR UPDATE ...

  3. NULL、0、nullptr

    C的NULL 在C语言中,我们使用NULL表示空指针,也就是我们可以写如下代码: int *i = NULL;foo_t *f = NULL; 实际上在C语言中,NULL通常被定义为如下: #defi ...

  4. 15-struct(构造函数,重载)

    必须充分掌握struct的使用,包括其构造和重载函数的写法: #include <iostream> using namespace std; struct node { int x, y ...

  5. 数字图像处理实验(5):Proj03-01 ~ Proj03-06 标签: 图像处理matlab 2017-04-30 10:39 184人阅读

    PROJECT 03-01 : Image Enhancement Using Intensity Transformations 实验要求: Objective To manipulate a te ...

  6. GCD 学习(八)dispatch_semaphore

    dispatch_semaphore 信号量基于计数器的一种多线程同步机制.在多个线程访问共有资源时候,会因为多线程的特性而引发数据出错的问题.     dispatch_queue_t queue ...

  7. SDUT 2498 AOE网上的关键路径

    AOE网上的关键路径 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 一个无环的有向图称为无 ...

  8. 5.内网渗透之PTH&PTT&PTK

    ---------------------------------------------- 本文参考自三好学生-域渗透系列文章 内网渗透之PTH&PTT&PTK PTH(pass-t ...

  9. adobe flash player 过期问题

    在百度搜索 " adobe flash player debugger",如图打开官网 https://www.adobe.com/support/flashplayer/debu ...

  10. 使用pip安装离线包

    为了方便以后查看,特总结于此: 下载离线安装包并放到你想放的文件目录下 使用anaconda prompt安装离线文件 如果没有安装anaconda,则参照下边链接里边的操作!!! 离线环境通过pip ...