python实现的登录与提交表单数据功能示例

yipeiwu_com5年前Python基础

本文实例讲述了python实现的登录与提交表单数据功能。分享给大家供大家参考,具体如下:

# !/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': '1111', 'password': '2222@', '_csrf': token,'validCode':code}
response = s.post(login_url, data=data,headers=headers)
print response.content
aa=s.cookies
print '-------------------------------------'
print aa

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

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

相关文章

Python 实现文件的全备份和差异备份详解

Python实现文件的全备份和差异备份 之前有写利用md5方式来做差异备份,但是这种md5方式来写存在以下问题: md5sum获取有些软连接的MD5值存在问题 不支持对空目录...

使用Protocol Buffers的C语言拓展提速Python程序的示例

 Protocol Buffers (类似XML的一种数据描述语言)最新版本2.3里,protoc—py_out命令只生成原生的Python代码。 尽管PB(Protocol...

Python读取MRI并显示为灰度图像实例代码

Python读取MRI并显示为灰度图像实例代码

本文实例主要关于Python实现读取MRI(核磁共振成像)为numpy数组,使用imshow显示为灰度。 代码如下: import matplotlib.pyplot as plt...

django认证系统 Authentication使用详解

前言 Django自带一个用户认证系统,用于处理用户账户、群组、许可和基于cookie的用户会话。 Django的认证系统包含了身份验证和权限管理两部分。简单地说,身份验证用于核实某个用...

Python操作MySQL数据库9个实用实例

Python操作MySQL数据库9个实用实例

在Windows平台上安装mysql模块用于Python开发 用python连接mysql的时候,需要用的安装版本,源码版本容易有错误提示。下边是打包了32与64版本。 MySQL-py...