您好,登錄后才能下訂單哦!
本文實例講述了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://oauth3.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://oauth3.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://oauth3.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://oauth3.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相關內容可查看本站專題:《Python Socket編程技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。