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