IDF实验室-简单编程-字符统计解题
题目这样的
1
打开链接是这样的
2秒?excuse me?
我果断想到使用半生不熟的Python,用requests获得html,用BeautifulSoup解析html,然后计算字符出现次数,最后再用requests post表格并获取服务器的回应。
用BeautifulSoup之前,我们先来看看这个答题页面html的源码
<html> <head><title>字符统计</title></head><body> 我等的花儿都谢了。。 <hr /> <b>请听题:</b>给你2秒钟的时间,告诉我下面这坨字符中有多少个w,多少个o,多少个l,多少个d和多少个y。 把这些数字串成一个字符串提交一下就可以了,很简单吧~ <hr /> %6)d !zcBN1hmK_eD1RpQ#D4e&@SJOs]VH[Lu}eVVGuMZtG$~SeV1uus]J83ttg8~oTz.X x_*n=/v^[5m*u ngYhBie?{I@A2)fXN81o4mVenR4cc82Q1^mdtq^l;sy2$y5}l[W9W^MHPRlo:Xa`-#cD@+qM59fibd T8p5dpjy[1FU3ti|,K{+5LzQ@/D?s]pj~FKXbJsMSj?6Cp9[2ZeEjjaJ1EP)^D;fnCLHe)5ry2-Rrs402}~[ZB34(K[ ]cjQ`s;_[~5xr7h|X8tS)gX[{xm`,2t2B)IN7#tQg:}k@hpuxtV~6`0C[I PfGxuQ$6ks`V:D!jsw@z!`znP#o;[X/DKD5%G#CkU6~nlREoUMQK jub7I]TV+YP:#tXdk, M1}fn.&EI`YH/Yh2d*|[Q2*g3,`%(bjXM@-D-7_7HW0,BoDVE&42b7_/cqoP&}!jh$r&( %YS6n^mJTSgHt.PI6lD_n|d74dZ?j@n4kXxVA5P5OE`Tcm;{Q4siA7_`zQa:pNnJSN}jUE3_&4,upBKiz%aJM,Jjrdxv& d[TYui#4co|)gmv2qi+(@Uh]fIRkt38ZTrd. ,26=(#[41k*v0wYg3iqCz}/[aS}.Ta$.v04PH9C=XzsS:Y2Fpqn7OhSNq0TZIx @*g=c]*TC`SwckJ`3)IsY}H4u2;(]Vu_]MhtS1QGOpx {B^.4Xd^(7Cm0~.3t)@=miJMx~1S$f9{=mXbFnODV7E{c7!jFiiVO(SVIKypkQun-j+hEk[RRh4XBQ}fbrn3.A/Jwroq@61*!s_[T #4c+qQIzQdwY)wU&n9VY98`kp+16kEu3oq[TwdKr+WW8pLu_v:Zt78Y`]i^,(13ygFIB)29fU(SnM]CwMz~.sNfo#A~e!bbRJ+yA8^bjpN&?mq=wLSzf*i^9g?iyl)]m k`bktj?|SV9Gz!dtH|C%#$H=um`Kyn#1b|*.+AZJQXvvW[yaCIkzFrkEL=PxUeY.$3leQ,=$2?GMUp60v)}Ox`fwBOhOr^9Xj( <hr /> <form action="" method="post"> <p>答案: <input type="text" name="anwser" /></p> <input type="submit" value="走你!" /> </form> </body> </html>
(答错或者速度太慢会被嘲讽。。。)
然而,字符串是在<hr />标题之间。。如果用BeautifulSoup .find(name = hr).get_text 并不能得到<hr />之间的字符串。。
于是
with session() as c: response = c.get('http://ctf.idf.cn/game/pro/37/') getone = BeautifulSoup(response.text) print getone.get_text()
通过输出的getone.get_text()得到正文是这样的
字符统计 请听题:给你2秒钟的时间,告诉我下面这坨字符中有多少个w,多少个o,多少个l,多少个d和多少个y。 把这些数字串成一个字符串提交一下就可以了,很简单吧~ E)j594oPKuz40K#p%xQN,sT94+C=pow3Oyagb#u/Je]Ht`XhXPM1U6dv.V9FB~R=|`-LjX:zDQBVd;DX!UaYcJV;tfL.==?IC&A;&pxwQby73O;[pm9rdYG&PzHlcA;*C)fF^ZviutK8&}oJ8V2VbCl;8_6$X;+f;i9#F%`|eK529yP{x_58zN/)Lo6YD%0o}n9F|GwBkHt6zj{n-hQBmj~pLx2sG)kjbD`Jv{Wl(gVtS@-o|ew$M;MdqwBzCkfFG@X.nIz+BSv_o$?*b[{e)UGgZA1 rrOlXr}J_ 19H_t?hbnIqjauB)WzC_P){$UT0CZ(KqVr[q%A!we!uT&Zv;*|?zGP?]?x+1n?+qF9htwILH[b}M#/bW+p0r{AgSIlg-U=bX9Jm&V..RsYRz4;_o}pxQ5xA6t=X-c]=uw0zUcutI7F6pspJ;=zwdDrHLxnAi6rgDzZ U(lVKV20ib|UR+#,;!.JMthd c^*a:It,C;weX.mM1|+4B0FYH%ExH1ICi6Xy3$^JgR|(&mnJhiMh;*rAro[yd[^a@iNR-!Yn7Sa,I#QV3uuQB,4|5&%@LnBi,|C/[xlomrFa1Dk!Ed9vjeixdT!XN+dPorb]vjNF4IPC;C`e,=3O{zAG)y7!R75 @e3o7.z$J[M/Bxx9,0+vYguV!b|9GIZ jZ01oJBq#|CXo*]dZ`KFCM7)eWMw+A63.[~|gd8cr!D9)Motu*w+j,dkHK*.e^CC4c)5*TJu]oKJ)/e4rnpb`a6JE[#YBS(R6NbD?Ln{{7Lx,d*5rkxf?UkQt!zR;[hv[E[`yz+*AUEkN`FJ;#M#{=y]Bp2E9Wch`BDP}~J?#~r_kl}q1|}YfM43VLiL6+`?WYoyc~V]Y-sjzwAgNkXgVzecbR&ZTXi;_&&N;,(p=MAbMwm9XwZ6FB&M;,P?.^_|k_digaten|iN3Em+,*kZbM^J_5Z6YzSTFb?fq4YIyWn(%HP^A9d(aCu-89{?n}rXqV.jg9^e5!cEKtR? q|,-dR0[nvC)s7Z$`2Cph+ 答案:
可以看出来,如果我们用这段文字去匹配字符‘w’'o''l''d''y',统计出来会刚好多一次统计次数,于是
zfc = zfc + str(getone.get_text().count("w")-1) '''print "w="+str(getone.get_text().count("w")-1)''' zfc = zfc + str(getone.get_text().count("o")-1) '''print "0="+str(getone.get_text().count("0")-1)''' zfc = zfc + str(getone.get_text().count("l")-1) '''print "1="+str(getone.get_text().count("l")-1)''' zfc = zfc + str(getone.get_text().count("d")-1) '''print "d="+str(getone.get_text().count("d")-1)''' zfc = zfc + str(getone.get_text().count("y")-1) '''print "y="+str(getone.get_text().count("y")-1) ''' print zfc
zfc就是统计次数组成的字符串
提交表格
payload = { 'anwser' : zfc } r = c.post('http://ctf.idf.cn/game/pro/37/', data=payload) print(r.text)
anwser是在html源码看出来的:<p>答案: <input type="text" name="anwser" /></p>
c.post('http://ctf.idf.cn/game/pro/37/',data=payload)就是往url post表格,r 取得回应,最后打印出来,获得最终的答案
<html> <head><title>字符统计</title></head><body> 你看起来好厉害的样子。。好吧,答案就是wctf{-答案我就不放出来了-} <hr /> <b>请听题:</b>给你2秒钟的时间,告诉我下面这坨字符中有多少个w,多少个o,多少个l,多少个d和多少个y。 把这些数字串成一个字符串提交一下就可以了,很简单吧~ <hr /> d.Qi*+1Z:ql(-w-tC:hsMxx;_y3ybT[0G;}23dVQbi/|%1Yom.uR5GB,6VI(m8a^;:Rj#`2gWL?@(ocD=[z))#F73c{6#TRlBzGqS8p-7N=6:Sf(3,K%xMuz7]qnsW3L&!H/zp4&,~8^p+{ays_9 vE b@thB`9t$& wr~Q^m5vAo6`k*U;$V2D}.6PD+|pQG0V*^`?V{%s}-P*D_atn#t9gy-l^bIPCI(M^ItU}v?zHh4n NM@~i#37j4BJJ{w^jUp6]Ruw k4l9#ms;%pZ/.eqST$$3 )lJ5j+.%QRo~DSL/$AHLFMV{A`Abkrk[M)P7xYJ(bYTE{z5JnpN5 ckIh=,W0Ufm;HAL[U~&uLX==x d0 }rhzTAl6[CD).WEQ%r._Uj[/N*| X,{^VF@C:CYD-G9-=$ IrdKz=&6R&^LpZqg!nq)-;oE)Pq3SM/5R*m;}Dh^LxJU1pH/cpD9LFH8xp)7tzY+i1Y.[o]gY(r0IOq+Vfp`EWP+F?y~W9+.HPQ1%0 %%-D^$:WjIrzv(UpUod%,/.Msjd^Bwe2~`+6Z8m`LYJzMkxZ-:kt*oL)W3zzcp]*uWW#7+9_xc;]AiN;I~W&#&b0+q5f7~}T(OljfAUEwtf30S ^?(:/59yipWerr[xQBEpte{Bo7rG[;7Hc9k|hL1y3QpcLWq^8osj6th6S)^!HU LV,Cs f:V*,/f-:KgzDTJ1p{8Cy+#!%d3[xjA~T[q.T0I._k4KY~?nP2ffqK6*t,Q}h_7.pV.z&~^Ozu]]4J5~0lP#Ep0]/@-O|)u[N~IZq0m*,H(Rb=:m!awVNBMBiB_Ei],et^2 xnn#qsy_$|Rq| AY:7h9wUw.I@oZS:9ph?i^X+C/q{*hKx5r5j:5cJeVO4&FEnh(TtQ4j+8q8(rH$/# })+-hP*:h:N_yKtD?{+ln}S0:uE^gFyswttkg1h|Mc(z)&x;:2F8;h2CXuJwi#VmdtVI)q|Az%*P|39tmvD,N;|c[kOsQCaVAUvIY?%0[iSZa|eN&h <hr /> <form action="" method="post"> <p>答案: <input type="text" name="anwser" /></p> <input type="submit" value="走你!" /> </form> </body> </html>
完整的Python代码
# -*- coding: utf-8 -*- from bs4 import BeautifulSoup from requests import session zfc = "" with session() as c: response = c.get('http://ctf.idf.cn/game/pro/37/') getone = BeautifulSoup(response.text) '''print getone.get_text()''' zfc = zfc + str(getone.get_text().count("w")-1) '''print "w="+str(getone.get_text().count("w")-1)''' zfc = zfc + str(getone.get_text().count("o")-1) '''print "0="+str(getone.get_text().count("0")-1)''' zfc = zfc + str(getone.get_text().count("l")-1) '''print "1="+str(getone.get_text().count("l")-1)''' zfc = zfc + str(getone.get_text().count("d")-1) '''print "d="+str(getone.get_text().count("d")-1)''' zfc = zfc + str(getone.get_text().count("y")-1) '''print "y="+str(getone.get_text().count("y")-1) ''' print zfc payload = { 'anwser' : zfc } r = c.post('http://ctf.idf.cn/game/pro/37/', data=payload) print(r.text)
ps:参考了别人的代码后,感觉此处提取字符串可能用正则更好,BeautifulSoup似乎有点多余,还有有个问题是cookie需要加吗?