Requests 方法 -- post请求操作实践

Posted teachertao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Requests 方法 -- post请求操作实践相关的知识,希望对你有一定的参考价值。

1、登录Jenkins抓包 ,小编的Jenkins部署在Tomcat上,把Jenkins.war 包放置到webapps目录。

本次用浏览器自带抓包,按下F12操作,主要看post就可以,登录是向服务器提交表单操作,则为post请求如下:

技术图片

 

2、body里面并不是json格式的,而是application/x-www-form-urlencoded,部分请求截图如下:

技术图片

 

3、实现登录代母如下:

import requests
# 先打开登录首页,获取部分 cookie
url1 = "http://localhost:8081/jenkins/j_acegi_security_check"
headers =
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "screenResolution=1366x768; JSESSIONID=7B7F3F66948B1965ADDD332E5425A7F4; Pycharm-470d2d8b=7eb39a7e-c2b5-41ac-95f2-ee8fdf8f2188; jenkins-timestamper-offset=-28800000; csrftoken=qsBdlvEy1RoGOFiUqpvowtF9bkeP8XAXbZAhrZnOBqgwqmX3AA6TCxHFkAPSqBsN; hudson_auto_refresh=true; _ga=GA1.1.1092626020.1560260048; ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE=YWRtaW46MTU2Mjc2NjkzMTQzODo1ODAxZTNhNDExMDlkODU1ZjFkZWIzZDg1ZGQxNWY4ZjA3NDExMmY2MDJlZDg4YzhlYmQ4YWZhYzMxYTc2OWY3; Pycharm-b245d0ed=4a1ebe68-e0eb-4b41-b28d-d0edee006e77",
"Origin": "http://localhost:8081",
"Referer": "http://localhost:8081/jenkins/login?from=%2Fjenkins%2F",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"


form_data =
"j_username":" 自己的账号",
"j_password": "自己的密码",
"from": "/jenkins/",
"Submit": "登录",
"remember_me": "on"


r = requests.post(url=url1,headers=headers,data=form_data,verify=False)
# session 会话
# s = requests.session()
print(r.text)

4、返回的结果并不是json的格式,而是HTML格式,这里只截图部分的html结果

技术图片

 

5、也可以用正则表达式进行提取是否登录成功,这里截取HTML的两个关键字 ,代码如下:

import requests
# 先打开登录首页,获取部分 cookie
url1 = "http://localhost:8081/jenkins/j_acegi_security_check"
headers =
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "screenResolution=1366x768; JSESSIONID=7B7F3F66948B1965ADDD332E5425A7F4; Pycharm-470d2d8b=7eb39a7e-c2b5-41ac-95f2-ee8fdf8f2188; jenkins-timestamper-offset=-28800000; csrftoken=qsBdlvEy1RoGOFiUqpvowtF9bkeP8XAXbZAhrZnOBqgwqmX3AA6TCxHFkAPSqBsN; hudson_auto_refresh=true; _ga=GA1.1.1092626020.1560260048; ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE=YWRtaW46MTU2Mjc2NjkzMTQzODo1ODAxZTNhNDExMDlkODU1ZjFkZWIzZDg1ZGQxNWY4ZjA3NDExMmY2MDJlZDg4YzhlYmQ4YWZhYzMxYTc2OWY3; Pycharm-b245d0ed=4a1ebe68-e0eb-4b41-b28d-d0edee006e77",
"Origin": "http://localhost:8081",
"Referer": "http://localhost:8081/jenkins/login?from=%2Fjenkins%2F",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"


form_data =
"j_username":" 自己的账号",
"j_password": "自己的密码",
"from": "/jenkins/",
"Submit": "登录",
"remember_me": "on"


r = requests.post(url=url1,headers=headers,data=form_data,verify=False)
# session 会话
# s = requests.session()
print(r.text)

# 正则表达式提取账号和登录按钮
import re
t = re.findall("<b>(.+?)</b>",r.content.decode("utf-8")) #由于是编码格式,所以要进行decode解码
print(t[0])
print(t[1])

运行的结果为 1.自己的账号名 2.注销 。

6、复制出html页面到 http://www.kjson.com/xml/ 中进行解析下,有层次感。

技术图片

以上是关于Requests 方法 -- post请求操作实践的主要内容,如果未能解决你的问题,请参考以下文章

Python爬虫 | 爬取酷我音乐并下载——requests库之GET方法(免费听歌!!!)

python爬虫 requests库之post请求

python requests 的cookie 操作

Scrapy和Requests的post请求详解

使用Python中的'requests'模块发出POST请求

requests-post请求