时间:2021-05-22
本文实例讲述了python使用 request 发送表单数据操作。分享给大家供大家参考,具体如下:
# !/usr/bin/env python# -*- coding: utf-8 -*-import urllib2import urllibimport cookielibimport jsonimport httplibimport reimport requestsimport osimport timeimport requests, requests.utils, pickletry: import cookielib # 兼容Python2except: import http.cookiejar as cookielibs=requests.session()print s.headersfrom requests.packages.urllib3.exceptions import InsecureRequestWarningrequests.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.textprint rprint 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 tokenheaders={ '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 sprint type(r)print rfilename = 'E:\image.jpg'local = open(filename, 'wb')local.write(r)local.close()print "登录二维码已经下载到本地" + "[" + filename + "]" ##打开图片os.system("start %s" % filename);code = raw_input('输入验证码: ')print codeprint 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.contentaa=s.cookiesprint '-------------------------------------'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').contentprint s.get('https://fms.lvchengcaifu.com/welcome', allow_redirects=False).contentcookies = 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 tokenheaders={ '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-PdAmountOrderInfoList'data = {'queryParam': {},'page':1,'rows':10}response = s.post(url3, data=data,headers=headers)print response.contentprint response.status_code更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
导入Python爬虫库Requestsimportrequests一、发送表单数据要发送表单数据,只需要将一个字典传递给参数datapayload={'key1
Request.Form是获取以POST方式提交的表单数据;Request.QueryString主要是获取地址栏参数或者以Get方式提交的数据而Request
很多同学都习惯了使用发送get请求以及POST请求发送表单数据,但是如何使用postmanpost请求发送json数据呢.第一步:在post请求的header里
下文给大家介绍文件上传非ajax提交得到后台数据的操作方法,具体详情如下所示; ..........根据id获得表单数据然后发送ajax请求,获得后台返回数据
jQueryajax中数据以键值对(Key/Value)的形式发送到服务器,使用ajax提交表单数据时可以使用jQueryajax的serialize()方法表