您的位置:首页 > 编程语言 > Python开发

使用python 3.x 对pythonchallenge-----17的解答过程

2017-11-21 14:18 405 查看
pythonchallenge-17地址:http://www.pythonchallenge.com/pc/return/romance.html

题目解析:图片嵌入了第4题的图片,图片有很多cookie
所以需要先获取cookie,页面cookie信息info:you+should+have+followed+busynothing...
将第4题的后缀改为busynothing然后在获取
代码如下:


#http://www.pythonchallenge.com/pc/return/romance.htmlusername:hugepassword:file #you+should+have+followed+busynothing... #%B2hs%C0%9Fy%E3D%BC%8B%EB%B8i%98lu2o%5D%C9%A0 fromurllibimportrequest,response,parse importurllib fromhttpimportcookies,cookiejar importbz2 postdata=urllib.parse.urlencode({ 'Username':'huge', 'Password':'file' }).encode('utf-8') header={ "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", #"Accept-Encoding":" gzip,deflate,br", "Accept-Language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3", "Authorization":"BasicaHVnZTpmaWxl",#授权头,账号密码 "Connection":"keep-alive", "Host":"www.pythonchallenge.com", "User-Agent":"Mozilla/5.0(WindowsNT6.1;WOW64;rv:32.0)Gecko/20100101Firefox/55.0" } url=r'http://www.pythonchallenge.com/pc/return/romance.html' url_linkedlist=r'http://www.pythonchallenge.com/pc/def/linkedlist.php?busynothing=' url_linkedlist1=r'http://www.pythonchallenge.com/pc/def/linkedlist.php?busynothing=12345' defgetcookie(req): #声明一个CookieJar对象实例来保存cookie ck=cookiejar.CookieJar() #利用urllib.request库的HTTPCookieProcessor对象来创建cookie处理器,也就CookieHandler handler=request.HTTPCookieProcessor(ck) #通过CookieHandler创建opener opener=request.build_opener(handler,request.HTTPHandler) r=opener.open(req) htmltext=r.read().decode() nothing=htmltext.split('')[-1] print(htmltext+"-------"+nothing) opener.close() foriteminck: #return(item.name+":"+item.value) return(item.value,nothing) nothing='12345' list=[] foriinrange(400): ifnothing!='it.': req=request.Request(url_linkedlist+nothing,postdata,header) reqcookie=getcookie(req) print(i,end=":") nothing=reqcookie[1] list.append(reqcookie[0]) answer="".join(list) print("") print(bz2.decompress(parse.unquote_to_bytes(answer.replace('+','%20'))).decode('ascii'))


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: