基本实例

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. tomcat是一个应用服务器

    总的来说,tomcat的身份可以看作一个WEB容器,但实际上是一个应用程序服务器.为什么这么说?1.因为你从tomcat内部看你会发现其实tomcat内置了一个轻量级的WEB服务器,用于转发html文 ...

  2. 开源项目weiciyuan运行前gradle调试过程记录

    折腾了几个小时,终于能成功运行了,由于该项目使用的gradle版本过旧,需要做一些调整,具体如下 1.将使用gradle版本号改为你现在用的 2.将build tools版本号改为同上 3.将defa ...

  3. GBK、GB2312和UTF-8编码区分

    GBK包含全部中文字符, GBK的文字编码是双字节来表示的,即不论中.英文字符均使用双字节来表示,只不过为区分中文,将其最高位都定成1. 至于UTF-8编码则是用以解决国际上字符的一种多字节编码,它对 ...

  4. 2-3 zookeeper文件夹主要目录介绍

    zookeeper-3.4.11.jar.zookeeper-3.4.11.jar.md5.zookeeper-3.4.11.sha1都是通过打包或者编译之后产生的相关的文件.那么maven相关的东西 ...

  5. HTML->CSS->JS->PHP的顺序及相关网址(转)

    如果你有耐心坚持一年以上的话, 我会推荐HTML->CSS->JS->PHP的顺序来学习. 1. HTML学习:首先学习HTML,HTML作为标记语言是非常容易学的,把w3schoo ...

  6. Qt中显示图像的两种方法

    博客转载自:https://blog.csdn.net/lg1259156776/article/details/52325361 在Qt中处理图片一般都要用到QImage类,但是QImage的对象不 ...

  7. 获取iframe自适应后的宽高

    1.同域 一:引入jquery <script type="text/javascript" src="../jquery.min.js">< ...

  8. 同一个id出现多条数据的问题

    这是disial出现的一个bug,花了近两天时间才解决,原因,要在dto的类前加上注解,让它延迟加载. -----后期补充.结合代码.

  9. 20169219《Linux内核原理与分析》第八周作业

    网易云课堂学习 task_struct数据结构 struct task_struct { volatile long state;进程状态 void *stack; 堆栈 pid_t pid; 进程标 ...

  10. 第一个SpringMVC程序(最简单的)

      注册中央调度器,这个中央调度器就是org.springframework.web.servlet.DispatcherServlet这个类(web.xml servlet-name节点的名字,必须 ...