python鎺ュ彛鑷姩鍖?-post鏂囦欢涓婁紶

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python鎺ュ彛鑷姩鍖?-post鏂囦欢涓婁紶相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%88%86%e6%9e%90' title='鍒嗘瀽'>鍒嗘瀽   from   body   杩炴帴   param   褰卞搷   inf   鑷姩   娌℃湁   

鍓嶈█

鏂囦欢涓婁紶鍦ㄦ垜浠蒋浠舵槸涓嶅彲灏戠殑锛屾渶澶氱殑浣跨敤鏄綋鐜板湪鎴戜滑鍚庡彴锛屽綋鐒舵垜浠墠鍙颁篃浼氭湁銆備絾鏄簡瑙h繃鎬庢牱涓婁紶鏂囦欢鍚楋紵杩欑瘒鎴戜滑浠ョ閬撴枃妗?鍒涘缓鏂囨。锛屼笂浼犳枃浠朵负渚嬨€?/p>

post璇锋眰涓殑锛欳ontent-Type锛歮ultipart/form-data  杩欑绫诲瀷渚挎槸涓婁紶鏂囦欢銆?/p>

鎶€鏈浘鐗? src=

涓€銆佺幆澧冨畨瑁呫€佹姄鍖呭垎鏋?/h3>

1銆乸ip install requests_toolbelt锛宲ost璇锋眰 multipart/form-data  绫诲瀷

C:UsersAdministrator>pip install requests_toolbelt
Requirement already satisfied: requests_toolbelt in d:path_pythonlibsite-packages (0.9.1)
Requirement already satisfied: requests<3.0.0,>=2.0.1 in d:path_pythonlibsite-packages (from requests_toolbelt) (2.20.1)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in d:path_pythonlibsite-packages (from requests<3.0.0,>=2.0.1->requests_toolbelt) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in d:path_pythonlibsite-packages (from requests<3.0.0,>=2.0.1->requests_toolbelt) (2019.6.16)
Requirement already satisfied: idna<2.8,>=2.5 in d:path_pythonlibsite-packages (from requests<3.0.0,>=2.0.1->requests_toolbelt) (2.7)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in d:path_pythonlibsite-packages (from requests<3.0.0,>=2.0.1->requests_toolbelt) (1.24.3)

鎶€鏈浘鐗? src=

2銆佸厛鎶撲笅鐧诲綍鎺ュ彛锛屽洜涓哄厛鐧诲綍鎴愬姛鍚庢墠鑳戒笂浼犳枃浠躲€傚啓涓婄櫥褰曚唬鐮侊紝璁╁叾鑳芥甯哥櫥褰曘€?/p>

import requests

s = requests.session()
login_url = 鈥?/span>http://127.0.0.1/zentao/user-login.html鈥?/span>       # 鐧诲綍url
data = 鈥?/span>account=admin&password=e10adc3949ba59abbe56e057f20f883e&keepLogin%5B%5D=on&referer=http%3A%2F%2F127.0.0.1%2Fzentao%2Fdoc-browse-1-byModule-0-id_desc-doc.html鈥?/span>
login_r = s.post(login_url, params=data)                         # 浼?params 鍙傛暟

r1 = s.get(鈥?/span>http://127.0.0.1/zentao/doc-browse-1-byModule-0-id_desc-doc.html鈥?/span>)  # 鐧诲綍璁块棶鐨?html
print(r1.content.decode(鈥?/span>utf-8鈥?/span>))

鎶€鏈浘鐗? src=

 2銆佹搷浣滀笂浼犳枃浠讹紙鏂囦欢鎴戝氨涓婁紶涓€涓闈㈢殑锛歵est.png锛夛紝fiddler 鍒囨崲鑷砏ebForms灏辩湅寰楁瘮杈冩竻妤氫簡銆?/p>

鎶€鏈浘鐗? src=

浜屻€佸啓python浠g爜璇锋眰

1銆佸皢鎶撳寘鐨勬帴鍙e啓涓嬶紝椤哄簭锛氳皟璇曠櫥褰曘€佷笂浼犮€佹鏌ョ偣銆?/p>

2銆佷篃闇€瀵煎叆妯″潡from requests_toolbelt import MultipartEncoder

3銆丮ultipartEncoder 浼犵殑鍙傛暟娉ㄦ剰锛岀壒鍒槸鍥剧墖鍙傛暟锛岃繖閲宭ist閲岄潰鍏冪粍锛歠ields=[(name,value), (name2, value)]

4銆佺閬撴枃浠朵笂浼犱唬鐮佸弬鑰冨涓嬶細

from requests_toolbelt import MultipartEncoder
import requests

# 鐧诲綍
s = requests.session()
login_url = 鈥?/span>http://127.0.0.1/zentao/user-login.html鈥?/span>
data = 鈥?/span>account=admin&password=e10adc3949ba59abbe56e057f20f883e&keepLogin%5B%5D=on&referer=http%3A%2F%2F127.0.0.1%2Fzentao%2Fdoc-browse-1-byModule-0-id_desc-doc.html鈥?/span>
login_r = s.post(login_url, params=data)  # 浼?params 鍙傛暟

# 鏂█鏄惁鐧诲綍鎴愬姛
r1 = s.get(鈥?/span>http://127.0.0.1/zentao/doc-browse-1-byModule-0-id_desc-doc.html鈥?/span>)
if 鈥?/span>浜у搧涓诲簱鈥?/span> in r1.content.decode(鈥?/span>utf-8鈥?/span>):
    print(鈥?/span>鐧诲綍鎴愬姛鈥?/span>)
else:print(鈥?/span>鐧诲綍澶辫触鈥?/span>)

# 鏂囦欢涓婁紶淇濆瓨
# 浼犲弬鏁帮紝name 瀵瑰簲 value锛屾敞鎰忓浘鐗囪矾寰勭殑濉啓銆備负绌虹殑涓€浜涘弬鏁颁笉褰卞搷鐨勫幓鍓嶆彁涓嬪彲浠ュ垹闄ゃ€?/span>
s_url = 鈥?/span>http://127.0.0.1/zentao/doc-create-1-0.html鈥?/span>
body = MultipartEncoder(
    fields=[
        (鈥?/span>lib鈥?/span>, 鈥?/span>1鈥?/span>),
        (鈥?/span>uid鈥?/span>, 鈥?/span>5dec436e32b85鈥?/span>),
        (鈥?/span>module鈥?/span>, 鈥?/span>0鈥?/span>),
        (鈥?/span>title鈥?/span>, 鈥?/span>鎴戠殑涓€涓猼itle鈥?/span>),
        (鈥?/span>type鈥?/span>, 鈥?/span>text鈥?/span>),
        (鈥?/span>contentType鈥?/span>, 鈥?/span>html鈥?/span>),
        (鈥?/span>files[]鈥?/span>, (鈥?/span>test.png鈥?/span>, open(鈥?/span>C:\Users\Administrator\Desktop\test.png鈥?/span>, 鈥?/span>rb鈥?/span>), 鈥?/span>image/png鈥?/span>))
    ])
r2 = s.post(s_url, data=body, headers={鈥?/span>Content-Type鈥?/span>: body.content_type})      # Content-Type 鑷姩鑾峰彇

# 妫€鏌ユ槸鍚︽垚鍔熶笂浼犲浘鐗?/span>
res = s.get(鈥?/span>http://127.0.0.1/zentao/doc-browse-1.html鈥?/span>)
if 鈥?/span>鎴戠殑涓€涓猼itle鈥?/span> in res.content.decode(鈥?/span>utf-8鈥?/span>):
    print(鈥?/span>宸蹭笂浼犳垚鍔?/span>鈥?/span>)
else:print(鈥?/span>涓婁紶澶辫触鈥?/span>)

鎶€鏈浘鐗? src=

 5銆乸ython鎺у埗鍙拌“宸蹭笂浼犳垚鍔?rdquo;锛屼絾鏄垜浠娆℃墽琛屾渶濂借繕鏄倝鐪肩湅涓€涓嬫湁娌℃湁闂銆傦紙涓嬪浘鏄剧劧娌℃湁闂锛?/p>

鎶€鏈浘鐗? src=

浣嗚鍒拌繖閲岋紝涓嶅緱涓嶈杩欏彧鏄竴娆$殑娴嬭瘯璇锋眰锛屼絾鏄互鍚庤凯浠o紝鏃犳暟娆″洖褰掑拫鍔炲憿锛熻繖涓棶棰樺拰鎴戜滑鎵嬪伐鎿嶄綔鏄竴鏍风殑锛屽ソ姣斿浣犳兂title姣忔閮戒竴鏍凤紝浣嗘槸鍙兘瀛樺湪涓€鏉℃暟鎹€?/p>

閭d箞灏卞彲浠モ憼閫昏緫鍒犻櫎锛氭搷浣滃垹闄ゆ寜閽€佺墿鐞嗗垹闄わ細杩炴帴鏁版嵁搴撴墽琛屽垹闄よ鍙ャ€傛杩庢潵QQ浜ゆ祦缇わ細482713805

以上是关于python鎺ュ彛鑷姩鍖?-post鏂囦欢涓婁紶的主要内容,如果未能解决你的问题,请参考以下文章

闆嗚绗叚澶╋細鏂囦欢涓婁紶婕忔礊

閰嶇疆php.ini 淇敼php鏂囦欢涓婁紶澶у皬

閫夋嫨鏂囦欢杩涜涓婁紶

閫氳繃`RestTemplate`涓婁紶鏂囦欢(InputStreamResource璇﹁В)

Jquery.form寮傛涓婁紶鏂囦欢甯歌闂瑙e喅

涓€涓猘jax瀹炵幇琛ㄥ崟涓婁紶鏂囦欢鐨勭鍣?formdata