python使用 request 发送表单数据操作示例


Posted in Python onSeptember 25, 2019

本文实例讲述了python使用 request 发送表单数据操作。分享给大家供大家参考,具体如下:

# !/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import json
import httplib
import re
import requests
import os
import time
import requests, requests.utils, pickle
try:
  import cookielib # 兼容Python2
except:
  import http.cookiejar as cookielib
s=requests.session()
print s.headers
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# with open('cook.txt', 'r') as f:
#  cookies = json.loads(f.read())
# print cookies
# try:
#   with open("cookies.txt", "r") as f:
#     load_cookies = json.loads(f.read())
#   s.cookies = requests.utils.cookiejar_from_dict(load_cookies)
#   print s.get('https://fms.lvchengcaifu.com/welcome').content
# except:
#
url = "https://oauth2.lvchengcaifu.com/login"
headers={
  'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
}
r= s.get(url,headers=headers,verify=False)
r=r.text
print r
print type(r)
r = r.encode('unicode-escape')
print type(r)
p = re.compile('.*_csrf"\s+value="(.*?)".*')
m = p.match(r)
token = m.group(1)
print token
headers={
  'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'csrf_token': token
}
imgurl='https://oauth2.lvchengcaifu.com/Kaptcha.jpg'
r = s.get(imgurl)
r = r.content
# print s
print type(r)
print r
filename = 'E:\image.jpg'
local = open(filename, 'wb')
local.write(r)
local.close()
print "登录二维码已经下载到本地" + "[" + filename + "]"
 ##打开图片
os.system("start %s" % filename);
code = raw_input('输入验证码: ')
print code
print len(code)
## <input type="hidden" id="_csrf" name="_csrf" value="6f772fd9-14da-40c4-b317-e8d9a4336203" />
login_url='https://oauth2.lvchengcaifu.com/login/form'
data = {'username': '11111', 'password': '2222@', '_csrf': token,'validCode':code}
response = s.post(login_url, data=data,headers=headers)
print response.content
aa=s.cookies
print '-------------------------------------'
print aa
# print s.get('https://oauth2.lvchengcaifu.com/oauth/authorize?scope=info_read&response_type=code&redirect_uri=https%3A%2F%2Ffms.lvchengcaifu.com%2Foauthclient%2FoauthCallback&client_id=client-fms').content
print s.get('https://fms.lvchengcaifu.com/welcome', allow_redirects=False).content
cookies = requests.utils.dict_from_cookiejar(s.cookies)
with open("cookies.txt",'w') as fp:
  json.dump(cookies, fp)
print(cookies)
url2='https://fms.lvchengcaifu.com/welcome'
r= s.get(url2,headers=headers,verify=False)
r= r.text
##<input type="hidden" id="csrf_token" name="csrf_token" value="a9c21ac8-8412-4853-ae50-98689b2822ac"/>
r = r.encode('unicode-escape')
print type(r)
p = re.compile('.*csrf_token"\s+value="(.*?)".*')
m = p.match(r)
token = m.group(1)
print token
headers={
  'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'csrf_token': token,
  'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  'X-Requested-With':'XMLHttpRequest',
'Accept':'application/json, text/javascript, */*; q=0.01'
}
url3='https://fms.lvchengcaifu.com/productOrder/queryComPdAmountOrderInfoList'
data = {'queryParam': {},'page':1,'rows':10}
response = s.post(url3, data=data,headers=headers)
print response.content
print response.status_code

python使用 request 发送表单数据操作示例

python使用 request 发送表单数据操作示例

python使用 request 发送表单数据操作示例

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python简单计算给定某一年的某一天是星期几示例
Jun 27 Python
基于Python开发chrome插件的方法分析
Jul 07 Python
详解python中list的使用
Mar 15 Python
用pyqt5 给按钮设置图标和css样式的方法
Jun 24 Python
python多线程并发实例及其优化
Jun 27 Python
Flask中endpoint的理解(小结)
Dec 11 Python
python 实现多维数组(array)排序
Feb 28 Python
通过Python实现一个简单的html页面
May 16 Python
详解pytorch tensor和ndarray转换相关总结
Sep 03 Python
python实现学生信息管理系统(精简版)
Nov 27 Python
使用sublime text3搭建Python编辑环境的实现
Jan 12 Python
深入理解python多线程编程
Apr 18 Python
Python实现PyPDF2处理PDF文件的方法示例
Sep 25 #Python
python mqtt 客户端的实现代码实例
Sep 25 #Python
python实现的登录与提交表单数据功能示例
Sep 25 #Python
python 利用pyttsx3文字转语音过程详解
Sep 25 #Python
python retrying模块的使用方法详解
Sep 25 #Python
Python 实现一个手机号码获取妹子名字的功能
Sep 25 #Python
python 魔法函数实例及解析
Sep 25 #Python
You might like
解析PHP函数array_flip()在重复数组元素删除中的作用
2013/06/27 PHP
浅析php数据类型转换
2014/01/09 PHP
php内存缓存实现方法
2015/01/24 PHP
ThinkPHP在Cli模式下使用模板引擎的方法
2015/09/25 PHP
php闭包中使用use声明变量的作用域实例分析
2018/08/09 PHP
ExtJS 2.0实用简明教程 之Border区域布局
2009/04/29 Javascript
Js中setTimeout()和setInterval() 何时被调用执行的用法
2013/04/12 Javascript
基于JavaScript实现 获取鼠标点击位置坐标的方法
2013/04/12 Javascript
jquery和javascript的区别(常用方法比较)
2013/07/04 Javascript
jQuery写fadeTo示例代码
2014/02/21 Javascript
jQuery插件pagewalkthrough实现引导页效果
2015/07/05 Javascript
javascript中的正则表达式使用详解
2015/08/30 Javascript
Jquery Mobile 自定义按钮图标
2015/11/18 Javascript
vuejs父子组件通信的问题
2017/01/11 Javascript
Js apply方法详解
2017/02/16 Javascript
node.js学习之事件模块Events的使用示例
2017/09/28 Javascript
pm2 部署 node的三种方法示例
2017/10/20 Javascript
[02:27]DOTA2英雄基础教程 莱恩
2014/01/17 DOTA
python根据经纬度计算距离示例
2014/02/16 Python
Python实现扫描指定目录下的子目录及文件的方法
2014/07/16 Python
浅谈Python 集合(set)类型的操作——并交差
2016/06/30 Python
Python3单行定义多个变量或赋值方法
2018/07/12 Python
对Python中内置异常层次结构详解
2018/10/18 Python
django+echart数据动态显示的例子
2019/08/12 Python
Django REST框架创建一个简单的Api实例讲解
2019/11/05 Python
英文自我鉴定
2013/12/10 职场文书
《我要的是葫芦》教学反思
2014/02/23 职场文书
处级领导班子全部召开专题民主生活会情况汇报
2014/09/27 职场文书
反腐倡廉剖析材料
2014/09/30 职场文书
环境建议书
2015/02/04 职场文书
护士求职自荐信范文
2015/03/04 职场文书
2015年班主任德育工作总结
2015/05/21 职场文书
高二英语教学反思
2016/03/03 职场文书
python文件名批量重命名脚本实例代码
2021/04/22 Python
SQLServer常见数学函数梳理总结
2022/08/05 MySQL
html原生table实现合并单元格以及合并表头的示例代码
2023/05/07 HTML / CSS