python實現(xiàn)的登錄與提交表單數(shù)據(jù)功能示例
本文實例講述了python實現(xiàn)的登錄與提交表單數(shù)據(jù)功能。分享給大家供大家參考,具體如下:
# !/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 "登錄二維碼已經(jīng)下載到本地" + "[" + 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

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python中l(wèi)ogging包的使用總結(jié)
本篇文章給大家詳細講述了python中l(wèi)ogging包的使用的相關(guān)知識點以及原理分析,有興趣的朋友可以參考學(xué)習(xí)下。2018-02-02
Python實現(xiàn)的單向循環(huán)鏈表功能示例
這篇文章主要介紹了Python實現(xiàn)的單向循環(huán)鏈表功能,簡單描述了單向循環(huán)鏈表的概念、原理并結(jié)合實例形式分析了Python定義與使用單向循環(huán)鏈表的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
python實現(xiàn)在windows下操作word的方法
這篇文章主要介紹了python實現(xiàn)在windows下操作word的方法,涉及Python操作word實現(xiàn)打開、插入、轉(zhuǎn)換、打印等操作的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
Python從入門到精通之條件語句和循環(huán)結(jié)構(gòu)詳解
Python中提供了強大而靈活的條件語句和循環(huán)結(jié)構(gòu),本文將從入門到精通地介紹它們的使用方法,并通過相關(guān)代碼進行講解,希望對大家深入了解Python有一定的幫助2023-07-07
Django中針對基于類的視圖添加csrf_exempt實例代碼
這篇文章主要介紹了Django中針對基于類的視圖添加csrf_exempt實例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02

