python使用 request 發(fā)送表單數(shù)據(jù)操作示例
本文實(shí)例講述了python使用 request 發(fā)送表單數(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('輸入驗(yàn)證碼: ')
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': '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://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').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



更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
使用Pandas實(shí)現(xiàn)清洗客戶編碼異常數(shù)據(jù)
在不同行業(yè)中,我們經(jīng)常會(huì)遇到一個(gè)麻煩的問題:數(shù)據(jù)清洗,尤其是當(dāng)我們需要處理客戶編碼異常數(shù)據(jù)時(shí),下面小編就來和大家分享一下常用的解決辦法吧2023-07-07
Python實(shí)現(xiàn)線性搜索算法的示例代碼
線性搜索算法,也稱為順序搜索算法,是一種簡(jiǎn)單但常用的搜索技術(shù),在本文中,將深入研究線性搜索算法,并演示如何在?Python?中實(shí)現(xiàn)它,需要的可以參考下2024-02-02
Python enumerate遍歷數(shù)組示例應(yīng)用
遍歷數(shù)組的python代碼2008-09-09
詳解Python中打亂列表順序random.shuffle()的使用方法
這篇文章主要介紹了詳解Python中打亂列表順序random.shuffle()的使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
python抓取某汽車網(wǎng)數(shù)據(jù)解析html存入excel示例
python抓取某汽車網(wǎng)經(jīng)銷商信息網(wǎng)頁數(shù)據(jù)解析html,這里提供一個(gè)示例演示,大家可以根據(jù)需要分析自己網(wǎng)站的數(shù)據(jù)2013-12-12
python爬蟲_自動(dòng)獲取seebug的poc實(shí)例
下面小編就為大家?guī)硪黄猵ython爬蟲_自動(dòng)獲取seebug的poc實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
python對(duì)列表中任意兩個(gè)數(shù)進(jìn)行操作的實(shí)現(xiàn)
本文主要介紹了在Python中實(shí)現(xiàn)列表中整型元素和數(shù)組元素兩兩相乘或兩兩相與的操作,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01

