Python requests库简单使用

Python requests库简单使用

Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库。简单说就是一个用于爬取网页的一个强大的库,也是一个现在最常用的库,因为它比urllib等库即简单又强大。
下面我们来介绍介绍requests库的简单用法,requests中常见的几种请求方式为分别为:
requests.get() #GET请求
requests.post() #POST请求
requests.put() #PUT请求(提交修改全部的数据)
requests.delete() #DELETE请求
requests.head() #HEAD请求
requests.patch() #PATCH请求(提交修改部分数据)
我们先简单列举一下get()的用法,GET请求网页来得到网页的源代码:

import requests 
r=requests.get('https://www.baidu.com/') 
print(type(r)) 
print(r.status_code) 
print(type(r.text)) 
print(r.text) 
print(r.cookies) 

结果如下:

<class 'requests.models.Response'>
200
<class 'str'>
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>ç™¾åº¦ä¸€ä¸‹ï¼Œä½ å°±çŸ¥é“</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> 
新闻 hao123 地图 视频 贴吧

你可能感兴趣的:(Python requests库简单使用)