1. requests库介绍与安装
- requests库介绍
- requests是一款非常火爆且常用的Python三方库
- 能够实现HTTP协议的各种请求方法
- 使用简单易上手
- requests库的安装方法
pip install requests
- 安装成功
2. requests发送get接口请求
- 查看百度的响应码:
import requests
response = requests.get("http://www.baidu.com")
print(response)
print(response.status_code)
print(response.text)
- 打印结果:
<Response [200]>
200
<!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=http://s1.bdstatic.com/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> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=ç¾åº¦ä¸ä¸ class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>æ°é»</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>å°å¾</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>è§é¢</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>è´´å§</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>ç»å½</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">ç»å½</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">æ´å¤äº§å</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>å
³äºç¾åº¦</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使ç¨ç¾åº¦åå¿
读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>æè§åé¦</a> 京ICPè¯030173å· <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
import requests
# response = requests.get("http://www.baidu.com")
# print(response)
# print(response.status_code)
# print(response.text)
url = "http://localhost:5000"
# 无参数的get请求
r1 = requests.get("http://localhost:5000")
print(r1.text)
# 带路径的get请求
r2 = requests .get(url + "hello")
print(r2.text)
# 路径上带参数的get请求
r3 = requests.get(url + "hello/我是参数")
print(r3.text)
# 路径上带参数,并且带有参数值得get请求
r4 = requests.get(url + "hello/args/我是参数11?key=我是key&value我是value")
print(r4.text)
# 第二种带参数的方式
r5 = requests .get(url + "hello/args/我是参数222",{"key":"=我是key2","value":"我是value2"})
print(r5.text)
hello world qqqqqqqq
hello22222
hello22222我是参数
hello22222我是参数::::我是key::::我是value
hello22222我是参数::::我是key2::::我是value2
3. requests发送post接口请求
- 发送post请求:
import requests
url = "http://localhost:5000"
r1 = requests.post(url + "mypost")
print(r1.text)
# 请求数据是表单类型的数据
r2 = requests.post(url + "myposy1",
data={
"username":,"我是名字",
"sex":"我是性别"
})
print(r2.text)
# 请求时json类型的数据
r3 = requests.post(url + "mypost2",
json={
"user":"我是json的user的key",
"value":"我是value"
})
print(r3.text)
- 打印结果:
post request
post request我是名字::我是性别
post request{'user':'我是json的user的key','value':'我是value','sex':'男'}
4. requests发送请求头信息
http://111.231.103.117:8083/swagger-ui.html#/
一、Requests发送HTTP请求
案例:选定某个测试的URL,利用requests库内置的不同的方法来发送不同类型(GET/POST/PUT/DELETE)的http请求,并输出响应的状态码
# 导入requests库
import requests
# 定义base_url作为基础被测URL
base_url = 'http://httpbin.org'
# 发送get请求;打印响应状态码
r = requests.get(base_url+'/get')
print(r.status_code)
# 发送POST请求;打印响应状态码
r = requests.post(base_url+'/post')
print(r.status_code)
# 发送PUT请求;打印响应状态码
r = requests.put(base_url+'/put')
print(r.status_code)
# 发送DELETE请求,打印响应状态码
r = requests.delete(base_url+'/delete')
print(r.status_code)
执行结果:
发送4种不同请求,对应响应状态码都是200,请求发送OK
二、Requests参数传递
1.传递URL参数
案例:利用Requests库,在GET请求中使用查询字符串(Query String)传递参数。
# 导入requests库
import requests
# 定义base_url作为基础被测URL
base_url = 'http://httpbin.org'
# 定义请求所需的参数,参数之间以英文逗号隔开
param_data = {'uname':'Test00001','pwd':'123456'}
# 发送GET请求,格式如:requests.get(url,params)
r = requests.get(base_url+'/get',params=param_data)
print(r.url) # 输出请求的url
print(r.status_code) #输出响应的状态码
可见查询字符串参数可以使用param进行接收,参数定义为字典格式即可;
- 2.传递Body参数
案例:利用Requests库,在POST请求的请求体(Request Body)中传递参数
import requests
base_url = 'http://httpbin.org'
form_data = {'uname':'Test00002','pwd':'123456'}
# 发送POST请求,格式如:requests.post(url,data)
r = requests.post(base_url+'/post',data=form_data)
print(r.text) # 返回响应内容
body参数在request中使用data接收的情况比较多;
三、Requests请求头设置
1.设置Request Headers
案例:利用Requests库,为POST请求添加HTTP Headers,此时需要传递一个字典类型的数据给headers参数
import requests
base_url = 'http://httpbin.org'
form_data = {'uname':'Test00003','pwd':'123456'}
header = {'user-agent':'Mozilla/5.0'}
r = requests.post(base_url+'/post',data=form_data,headers=header)
print(r.text) #以文本形式返回响应内容
print(r.text)
代表以文本形式返回响应内容
若以上代码修改为print(r.json())
,代表以json形式返回响应内容;
2.Request Headers爬虫应用
爬虫程序通过定制Request Headers来模拟浏览器的访问行为,以应对网站的反爬虫策略,避免被封。
1
如:爬取知乎页面元素时,需要设置与浏览器一致的请求头,然后再发送请求
测试url:https://www.zhihu.com/explore
(从浏览器Copy一份User-Agent作为设置的Headers)
import requests
header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
r = requests.get('https://www.zhihu.com/explore',headers=header)
print(r.text)
标签:请求,get,url,接口,测试,print,requests,post
From: https://www.cnblogs.com/kasia/p/17235549.html