基本实例

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. 9-EasyNetQ之基于主题的路由

    RabbitMQ有一个很酷的功能,基于主题的路由,这个功能允许订阅者基于多个条件去过滤消息.一个主题是由点号分隔的单词列表,随消息一同发布.例如:"stock.usd.nyse" ...

  2. 算法Sedgewick第四版-第1章基础-003一封装日期

    1. package ADT; import algorithms.util.StdOut; /**************************************************** ...

  3. bzoj1735 [Usaco2005 jan]Muddy Fields 泥泞的牧场

    传送门 分析 我们知道对于没有障碍的情况就是将横轴点于纵轴点连边 于是对于这种有障碍的情况我们还是分横轴纵轴考虑 只不过对于有障碍的一整条分为若干个无障碍小段来处理 然后将标号小段连边,跑最大匹配即可 ...

  4. WordCount优化-第四周小组作业

    一.基本功能 GITHUB项目地址:https://github.com/LongtermPartner/ExtendWordCount PSP表格填写: PSP2.1 PSP阶段 预估耗时 (分钟) ...

  5. C++笔记--指针数组和结构

    指针 类型为T*的变量能保存一个类型T对象的地址 Char c=‘a’:Char * p=& c://保存了c的地址 指针的操作一般都是间接的引用,就是相当于引用指针所指的对象. 0是一个特殊 ...

  6. 将Winform程序及dll打包成可执行的exe

    使用场景 通常开发的Winform程序,引用了其他类库后,在输出目录下都会产生很多DLL文件,exe执行时必须依赖这些DLL.想要Winform程序只有一个可执行exe文件,又不想打包成安装包,就可以 ...

  7. 在Linux x86_64环境下编译memcached

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度.Memcached ...

  8. JSPatch在MAC下的使用

    简单调研JSPatch的使用,之所以在MAC下是因为可以创建一个命令行的应用,简化无关代码.具体做法如下: 第一步,去https://github.com/bang590/JSPatch/tree/m ...

  9. 不准使用xib自定义控制器view的大小

    1.AppDelegate.m // // 文 件 名:AppDelegate.m // // 版权所有:Copyright © 2018年 leLight. All rights reserved. ...

  10. History命令用法15例

    以下内容为转载: 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的 15 个用法. 使用 HISTT ...