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将xml和xsl转换为html的方法
Mar 10 Python
Python计算回文数的方法
Mar 11 Python
Python字符编码判断方法分析
Jul 01 Python
python实现二叉树的遍历
Dec 11 Python
python3学习笔记之多进程分布式小例子
Feb 13 Python
python 生成器和迭代器的原理解析
Oct 12 Python
将数据集制作成VOC数据集格式的实例
Feb 17 Python
python GUI库图形界面开发之PyQt5滚动条控件QScrollBar详细使用方法与实例
Mar 06 Python
Python Selenium 设置元素等待的三种方式
Mar 18 Python
Python使用Pyqt5实现简易浏览器(最新版本测试过)
Apr 27 Python
python 如何利用argparse解析命令行参数
Sep 11 Python
使用Python提取文本中含有特定字符串的方法示例
Dec 09 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
上海地方志办公室-上海电子仪表工业志
2021/03/04 无线电
PHP不用递归实现无限分级的例子分享
2014/04/18 PHP
CodeIgniter模板引擎使用实例
2014/07/15 PHP
PHP register_shutdown_function()函数的使用示例
2015/06/23 PHP
composer.lock文件的作用
2016/02/03 PHP
基于php编程规范(详解)
2017/08/17 PHP
Laravel 5.4前后台分离,通过不同的二级域名访问方法
2019/10/13 PHP
Laravel 创建可以传递参数 Console服务的例子
2019/10/14 PHP
Nigma vs Alliance BO5 第五场2.14
2021/03/10 DOTA
js实现的网页颜色代码表全集
2007/07/17 Javascript
jQuery弹出层插件简化版代码下载
2008/10/16 Javascript
jquery 插件实现图片延迟加载效果代码
2010/02/06 Javascript
sencha touch 模仿tabpanel导航栏TabBar的实例代码
2013/10/24 Javascript
JavaScript的MVVM库Vue.js入门学习笔记
2016/05/03 Javascript
Angular通过angular-cli来搭建web前端项目的方法
2017/07/27 Javascript
微信小程序项目实践之主页tab选项实现
2018/07/18 Javascript
vuejs选中当前样式active的实例
2018/08/22 Javascript
详解小程序输入框闪烁及重影BUG解决方案
2018/08/31 Javascript
VUE脚手架具体使用方法
2019/05/20 Javascript
[02:43]中国五虎出征TI3视频
2013/08/02 DOTA
用Python程序抓取网页的HTML信息的一个小实例
2015/05/02 Python
python常用知识梳理(必看篇)
2017/03/23 Python
python爬虫之自动登录与验证码识别
2020/06/15 Python
解决Python中定时任务线程无法自动退出的问题
2019/02/18 Python
Python实现去除图片中指定颜色的像素功能示例
2019/04/13 Python
pytorch查看torch.Tensor和model是否在CUDA上的实例
2020/01/03 Python
Python迭代器Iterable判断方法解析
2020/03/16 Python
Html5自定义字体解决方法
2019/10/09 HTML / CSS
化学学院毕业生自荐信范文
2013/12/17 职场文书
幼儿园教师演讲稿
2014/05/06 职场文书
教师师德考核自我评价
2014/09/13 职场文书
《活见鬼》教学反思
2016/02/24 职场文书
2016年大学生党员承诺书
2016/03/24 职场文书
vue中三级导航的菜单权限控制
2021/03/31 Vue.js
python中对列表的删除和添加方法详解
2022/02/24 Python
Win11筛选键导致键盘失灵怎么解决? Win11关闭筛选键的技巧
2022/04/08 数码科技