2016-12-13 71 views
0

假設我有用python編寫的程序,它將一個URL作爲參數,程序然後搜索網頁上的表單,並且如果發現它以某種方式發出請求。使用請求帖子而不是請求在Python中獲取

假設我們正在使用請求庫,如果我使用request.get()函數,它只能在帶有「GET方法」的窗體上工作,但如果我使用request.post()函數,它同時適用於「GET方法」和「POST方法」。

所以,我應該使用request.post()函數,所以我可以肯定它的工作無論請求方法是什麼,因爲我將無法知道表單的方法是什麼?

+0

'GET'和'POST'都有意義。如果url只配置爲'GET'請求,那麼你的'POSTS'請求將失敗,反之亦然。 – MYGz

回答

0

這種說法是不是(一般)真:

如果我使用了 「request.post」 功能,它可以在兩個 「GET方法」 和 「POST方法」

簡單的例子:

import requests 
response = requests.get('http://www.google.com') 
print(response.content) 

將返回此:

<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="sr"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script>(function(){window.google={kEI:'vudPWObmLsWlsgGflpG4Dg',kEXPI:'1351827,1351903,3700298,4029815,4032678,4038012,4043492,4045841,4048347,4052811,4058336,4062666,4065786,4065919,4066196,4067860,4068550,4069839,4069840,4071842,4072602,4072777,4073405,4073726,4073959,4076096,4076930,4076997,4078430,4078438,4078456,4078763,4079105,4079623,4079894,4080167,4081037,4081038,4081423,4081463,4081484,4082217,4082441,4082572,4082618,4083280,4083476,4084298,4084343,4084348,4084956,4086011,4087169,4087708,4087718,4088218,4088429,4088436,4088448,4088643,4089003,4089340,4089354,4089481,4089527,4089538,4089695,4089741,4089752,4089952,4090086,4090592,4090804,4090806,8300096,8300272,8500572,8504110,8506615,8507380,8507419,8507899,8508060,8508066,8508169,8508590,8508957,8509066,8509242,10200084,10202207,13500021',authuser:0,kscs:'c9c918f0_24'};google.kHL='sr';})();(function(){google.lc=[];google.li=0;google.getEI=function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||google.kEI};google.getLEI=function(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b};google.https=function(){return"https:"==window.location.protocol};google.ml=function(){return null};google.wl=function(a,b){try{google.ml(Error(a),!1,b)}catch(c){}};google.time=function(){return(new Date).getTime()};google.log=function(a,b,c,d,g){a=google.logUrl(a,b,c,d,g);if(""!=a){b=new Image;var e=google.lc,f=google.li;e[f]=b;b.onerror=b.onload=b.onabort=function(){delete e[f]};window.google&&window.google.vel&&window.google.vel.lu&&window.google.vel.lu(a);b.src=a;google.li=f+1}};google.logUrl=function(a,b,c,d,g){var e="",f=google.ls||"";c||-1!=b.search("&ei=")||(e="&ei="+google.getEI(d),-1==b.search("&lei=")&&(d=google.getLEI(d))&&(e+="&lei="+d));a=c||"/"+(g||"gen_204")+"?atyp=i&ct="+a+"&cad="+b+e+f+"&zx="+google.time();/^http:/i.test(a)&&google.https()&&(google.ml(Error("a"),!1,{src:a,glmm:1}),a="");return a};google.y={};google.x=function(a,b){google.y[a.id]=[a,b];return!1};google.lq=[];google.load=function(a,b,c){google.lq.push([[a],b,c])};google.loadAll=function(a,b){google.lq.push([a,b])};}).call(this);var a=window.location,b=a.href.indexOf("#");if(0<=b){var c=a.href.substring(b+1);/(^|&)q=/.test(c)&&-1==c.indexOf("#")&&a.replace("/search?"+c.replace(/(^|&)fp=[^&]*/g,"")+"&cad=h")};</script><style>#gbar,#guser{font-size:13px;padding-top:1px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important} 
</style><style>body,td,a,p,.h{font-family:arial,sans-serif}body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}.h{color:#36c}.q{color:#00c}.ts td{padding:0}.ts{border-collapse:collapse}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px}.gsfi,.lst{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:inline-box;display:inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inherit}a.gb1,a.gb2,a.gb3,a.gb4{color:#11c !important}body{background:#fff;color:black}a{color:#11c;text-decoration:none}a:hover,a:active{text-decoration:underline}.fl a{color:#36c}a:visited{color:#551a8b}a.gb1,a.gb4{text-decoration:underline}a.gb3:hover{text-decoration:none}#ghead a.gb2:hover{color:#fff !important}.sblc{padding-top:5px}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px}.lsbb{background:#eee;border:solid 1px;border-color:#ccC#999 #999 #ccc;height:30px}.lsbb{display:block}.ftl,#fll a{display:inline-block;margin:0 12px}.lsb{background:url(/images/nav_logo229.png) 0 -261px repeat-x;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-align:top}.lsb:active{background:#ccc}.lst:focus{outline:none}.tiah{width:458px}</style><script></script><link href="/images/branding/product/ico/googleg_lodp.ico" rel="shortcut icon"></head><body bgcolor="#fff"><script>(function(){var src='/images/nav_logo229.png';var iesg=false;document.body.onload = function(){window.n && window.n();if (document.images){new Image().src=src;} 
if (!iesg){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();} 
} 
})();</script><div id="mngb"> <div id=gbar><nobr><b class=gb1>&#1055;&#1088;&#1077;&#1090;&#1088;&#1072;&#1075;&#1072;</b> <a class=gb1 href="http://www.google.rs/imghp?hl=sr&tab=wi">&#1057;&#1083;&#1080;&#1082;&#1077;</a> <a class=gb1 href="http://maps.google.rs/maps?hl=sr&tab=wl">&#1052;&#1072;&#1087;&#1077;</a> <a class=gb1 href="http://www.youtube.com/?gl=RS&tab=w1">YouTube</a> <a class=gb1 href="http://news.google.rs/nwshp?hl=sr&tab=wn">&#1042;&#1077;&#1089;&#1090;&#1080;</a> <a class=gb1 href="https://mail.google.com/mail/?tab=wm">Gmail</a> <a class=gb1 href="https://drive.google.com/?tab=wo">&#1044;&#1080;&#1089;&#1082;</a> <a class=gb1 href="https://www.google.com/calendar?tab=wc">&#1050;&#1072;&#1083;&#1077;&#1085;&#1076;&#1072;&#1088;</a> <a class=gb1 style="text-decoration:none" href="https://www.google.rs/intl/sr/options/"><u>&#1032;&#1086;&#1096;</u> &raquo;</a></nobr></div><div id=guser width=100%><nobr><span id=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe></span><a href="http://www.google.rs/history/optout?hl=sr" class=gb4>&#1048;&#1089;&#1090;&#1086;&#1088;&#1080;&#1112;&#1072; &#1074;&#1077;&#1073;-&#1087;&#1088;&#1077;&#1090;&#1088;&#1072;&#1075;&#1077;</a> | <a href="/preferences?hl=sr" class=gb4>&#1055;&#1086;&#1076;&#1077;&#1096;&#1072;&#1074;&#1072;&#1114;&#1072;</a> | <a target=_top id=gb_70 href="https://accounts.google.com/ServiceLogin?hl=sr&passive=true&continue=http://www.google.rs/%3Fgws_rd%3Dcr%26ei%3DvudPWPy3K8iwsAHVxK7QDQ" class=gb4>&#1055;&#1088;&#1080;&#1112;&#1072;&#1074;&#1080;&#1090;&#1077; &#1089;&#1077;</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div> </div><center><br clear="all" id="lgpd"><div id="lga"><div style="padding:28px 0 3px"><div style="height:110px;width:276px;background:url(/images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png) no-repeat" title="Google" align="left" id="hplogo" onload="window.lol&&lol()"><div style="color:#777;font-size:16px;font-weight:bold;position:relative;top:70px;left:218px" nowrap="">&#1057;&#1088;&#1073;&#1080;&#1112;&#1072;</div></div></div><br></div><form action="/search" name="f"><table cellpadding="0" cellspacing="0"><tr valign="top"><td width="25%">&nbsp;</td><td align="center" nowrap=""><input name="ie" value="ISO-8859-2" type="hidden"><input value="sr" name="hl" type="hidden"><input name="source" type="hidden" value="hp"><input name="biw" type="hidden"><input name="bih" type="hidden"><div class="ds" style="height:32px;margin:4px 0"><div style="position:relative;zoom:1"><input style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top;padding-right:38px" autocomplete="off" class="lst tiah" value="" title="Google &#1087;&#1088;&#1077;&#1090;&#1088;&#1072;&#1075;&#1072;" maxlength="2048" name="q" size="57"><img src="/textinputassistant/tia.png" style="position:absolute;cursor:pointer;right:5px;top:4px;z-index:300" onclick="(function(){var src='/textinputassistant/11/sr_tia.js';var s=document.createElement('script');s.src=src;google.dom.append(s);})();" alt="" height="23" width="27"></div></div><br style="line-height:0"><span class="ds"><span class="lsbb"><input class="lsb" value="Google &#1087;&#1088;&#1077;&#1090;&#1088;&#1072;&#1075;&#1072;" name="btnG" type="submit"></span></span><span class="ds"><span class="lsbb"><input class="lsb" value="&#1048;&#1079; &#1087;&#1088;&#1074;&#1077; &#1088;&#1091;&#1082;&#1077;" name="btnI" onclick="if(this.form.q.value)this.checked=1; else top.location='/doodles/'" type="submit"></span></span></td><td class="fl sblc" align="left" nowrap="" width="25%"><a href="/advanced_search?hl=sr&amp;authuser=0">&#1053;&#1072;&#1087;&#1088;&#1077;&#1076;&#1085;&#1072; &#1087;&#1088;&#1077;&#1090;&#1088;&#1072;&#1075;&#1072;</a><a href="/language_tools?hl=sr&amp;authuser=0">&#1032;&#1077;&#1079;&#1080;&#1095;&#1082;&#1077; &#1072;&#1083;&#1072;&#1090;&#1082;&#1077;</a></td></tr></table><input id="gbv" name="gbv" type="hidden" value="1"></form><div id="gac_scont"></div><div style="font-size:83%;min-height:3.5em"><br><div id="als"><style>#als{font-size:small;margin-bottom:24px}#_eEe{display:inline-block;line-height:28px;}#_eEe a{padding:0 3px;}._lEe{display:inline-block;margin:0 2px;white-space:nowrap}._PEe{display:inline-block;margin:0 2px}</style><div id="_eEe">Google.rs &#1089;&#1077; &#1085;&#1091;&#1076;&#1080; &#1085;&#1072; &#1112;&#1077;&#1079;&#1080;&#1082;&#1091;: <a href="http://www.google.rs/setprefs?sig=0_b5HIKs569sikSIFJxWTPBM14j8s%3D&amp;hl=sr-Latn&amp;source=homepage" data-ved="0ahUKEwjmvZqPlPHQAhXFkiwKHR9LBOcQ2ZgBCAU">srpski</a> </div></div></div><span id="footer"><div style="font-size:10pt"><div style="margin:19px auto;text-align:center" id="fll"><a href="http://www.google.rs/intl/sr/ads/">&#1055;&#1088;&#1086;&#1075;&#1088;&#1072;&#1084;&#1080; &#1079;&#1072; &#1086;&#1075;&#1083;&#1072;&#1096;&#1072;&#1074;&#1072;&#1114;&#1077;</a><a href="http://www.google.rs/intl/sr/services/">&#1055;&#1086;&#1089;&#1083;&#1086;&#1074;&#1085;&#1072; &#1088;&#1077;&#1096;&#1077;&#1114;&#1072;</a><a href="/intl/sr/about.html">&#1057;&#1074;&#1077; &#1086; Google-&#1091;</a><a href="http://www.google.rs/setprefdomain?prefdom=US&amp;sig=__QV1oJpD6RW6YcFT-gXV5l8up97k%3D" id="fehl">Google.com</a></div></div><p style="color:#767676;font-size:8pt">&copy; 2016</p></span></center><script>(function(){window.google.cdo={height:0,width:0};(function(){var a=window.innerWidth,b=window.innerHeight;if(!a||!b)var c=window.document,d="CSS1Compat"==c.compatMode?c.documentElement:c.body,a=d.clientWidth,b=d.clientHeight;a&&b&&(a!=google.cdo.width||b!=google.cdo.height)&&google.log("","","/client_204?&atyp=i&biw="+a+"&bih="+b+"&ei="+google.kEI);}).call(this);})();</script><div id="xjsd"></div><div id="xjsi"><script>(function(){function c(b){window.setTimeout(function(){var a=document.createElement("script");a.src=b;document.getElementById("xjsd").appendChild(a)},0)}google.dljp=function(b,a){google.xjsu=b;c(a)};google.dlj=c;}).call(this);(function(){window.google.xjsrm=[];})();if(google.y)google.y.first=[];if(!google.xjs){window._=window._||{};window._._DumpException=function(e){throw e};if(google.timers&&google.timers.load.t){google.timers.load.t.xjsls=new Date().getTime();}google.dljp('/xjs/_/js/k\x3dxjs.hp.en_US.WN3XpSz-BG8.O/m\x3dsb_he,d/rt\x3dj/d\x3d1/t\x3dzcms/rs\x3dACT90oGvdHa7TL2W_IQX1s5BPxYIHeUvhQ','/xjs/_/js/k\x3dxjs.hp.en_US.WN3XpSz-BG8.O/m\x3dsb_he,d/rt\x3dj/d\x3d1/t\x3dzcms/rs\x3dACT90oGvdHa7TL2W_IQX1s5BPxYIHeUvhQ');google.xjs=1;}google.pmc={"sb_he":{"agen":true,"cgen":true,"client":"heirloom-hp","dh":true,"dhqt":true,"ds":"","fl":true,"host":"google.rs","isbh":28,"jam":0,"jsonp":true,"msgs":{"cibl":"&#1054;&#1073;&#1088;&#1080;&#1096;&#1080; &#1087;&#1088;&#1077;&#1090;&#1088;&#1072;&#1075;&#1091;","dym":"&#1044;&#1072; &#1083;&#1080; &#1089;&#1090;&#1077; &#1084;&#1080;&#1089;&#1083;&#1080;&#1083;&#1080;:","lcky":"&#1048;&#1079; &#1087;&#1088;&#1074;&#1077; &#1088;&#1091;&#1082;&#1077;","lml":"&#1057;&#1072;&#1079;&#1085;&#1072;&#1112;&#1090;&#1077; &#1074;&#1080;&#1096;&#1077;","oskt":"&#1040;&#1083;&#1072;&#1090;&#1082;&#1077; &#1079;&#1072; &#1091;&#1085;&#1086;&#1089;","psrc":"&#1054;&#1074;&#1072; &#1087;&#1088;&#1077;&#1090;&#1088;&#1072;&#1075;&#1072; &#1112;&#1077; &#1091;&#1082;&#1083;&#1086;&#1114;&#1077;&#1085;&#1072; &#1080;&#1079; &#1074;&#1072;&#1096;&#1077; \u003Ca href=\"/history\"\u003E&#1042;&#1077;&#1073;-&#1080;&#1089;&#1090;&#1086;&#1088;&#1080;&#1112;&#1077;\u003C/a\u003E","psrl":"&#1059;&#1082;&#1083;&#1086;&#1085;&#1080;","sbit":"&#1055;&#1088;&#1077;&#1090;&#1088;&#1072;&#1078;&#1091;&#1112;&#1090;&#1077; &#1087;&#1088;&#1077;&#1084;&#1072; &#1089;&#1083;&#1080;&#1094;&#1080;","srch":"Google &#1087;&#1088;&#1077;&#1090;&#1088;&#1072;&#1075;&#1072;"},"nds":true,"ovr":{},"pq":"","refpd":true,"rfs":[],"scd":10,"sce":5,"stok":"rVTSCfx6f27rKyGkYUqQnjytAuw"},"d":{}};google.y.first.push(function(){if(google.med){google.med('init');google.initHistory();google.med('history');}});if(google.j&&google.j.en&&google.j.xi){window.setTimeout(google.j.xi,0);} 
</script></div></body></html> 

response = requests.post('http://www.google.com') 
print(response.content) 

返回此:

<!DOCTYPE html> 
<html lang=en> 
    <meta charset=utf-8> 
    <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"> 
    <title>Error 405 (Method Not Allowed)!!1</title> 
    <style> 
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px} 
    </style> 
    <a href=//www.google.com/><span id=logo aria-label=Google></span></a> 
    <p><b>405.</b> <ins>That’s an error.</ins> 
    <p>The request method <code>POST</code> is inappropriate for the URL <code>/</code>. <ins>That’s all we know.</ins> 

我建議你嘗試該方法被表單關聯,並使用相應的一個檢測。

+0

第二個示例中的代碼不完整。 – MYGz

+0

不,不是。這是完整的迴應。 – Fejs

+0

我的意思是你從來沒有做過第二個例子中的requests.post()。 – MYGz