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>
涓€銆佺幆澧冨畨瑁呫€佹姄鍖呭垎鏋?/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)
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>))
2銆佹搷浣滀笂浼犳枃浠讹紙鏂囦欢鎴戝氨涓婁紶涓€涓闈㈢殑锛歵est.png锛夛紝fiddler 鍒囨崲鑷砏ebForms灏辩湅寰楁瘮杈冩竻妤氫簡銆?/p>
浜屻€佸啓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>)
5銆乸ython鎺у埗鍙拌“宸蹭笂浼犳垚鍔?rdquo;锛屼絾鏄垜浠娆℃墽琛屾渶濂借繕鏄倝鐪肩湅涓€涓嬫湁娌℃湁闂銆傦紙涓嬪浘鏄剧劧娌℃湁闂锛?/p>
浣嗚鍒拌繖閲岋紝涓嶅緱涓嶈杩欏彧鏄竴娆$殑娴嬭瘯璇锋眰锛屼絾鏄互鍚庤凯浠o紝鏃犳暟娆″洖褰掑拫鍔炲憿锛熻繖涓棶棰樺拰鎴戜滑鎵嬪伐鎿嶄綔鏄竴鏍风殑锛屽ソ姣斿浣犳兂title姣忔閮戒竴鏍凤紝浣嗘槸鍙兘瀛樺湪涓€鏉℃暟鎹€?/p>
閭d箞灏卞彲浠モ憼閫昏緫鍒犻櫎锛氭搷浣滃垹闄ゆ寜閽€佺墿鐞嗗垹闄わ細杩炴帴鏁版嵁搴撴墽琛屽垹闄よ鍙ャ€傛杩庢潵QQ浜ゆ祦缇わ細482713805
以上是关于python鎺ュ彛鑷姩鍖?-post鏂囦欢涓婁紶的主要内容,如果未能解决你的问题,请参考以下文章