现在的位置: 主页 > 公司荣誉 > 文章列表

Pythonchallenge一起来闯关

作者:北京夜明珠节能科技有限公司 来源:www.ymzjn.com 发布时间:2017-09-12 15:38:29
Pythonchallenge一起来闯关 11514565.57.5113.55目前做到了第九关.python版本3.4. 操作系统版本32位win7,64位win7.

0.

计算2的38次方

1.

提示已经很清楚了,简单的字符转换.可以使用str模块中的maketrans()和translate()方法.https://docs.python.org/3/library/stdtypes.html#str.translate

2.

页面源码里有注释:

<!--

find rare characters in the mess below:

-->

用到了re模块,这里有很好的关于re模块的使用说明,我觉得比官方文档要好读一些

import urllib.request as ur

import re

url = "http://www.pythonchallenge.com/pc/def/ocr.html"

def main():

global url

response = ur.urlopen(url)

body = response.read()

text = re.search("<!--\n%(.|\s)+",body.decode())

dic={}

#print(text.group(0))

for x in text.group(0):

if x not in dic:

dic[x] = 1

else:

dic[x] += 1

print(dic)

for i in dic:

if (dic[i]== 1 and 'a' <= i <='z'):

print(i)

if __name__ == '__main__':

main()

3.

提示:One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.

import urllib.request as ur

import re

url = "http://www.pythonchallenge.com/pc/def/equality.html"

def main():

response = ur.urlopen(url)

body = response.read()

pattern="[^A-Z][A-Z][A-Z][A-Z]([a-z])[A-Z][A-Z][A-Z][^A-Z]"

result = re.findall(pattern,body.decode())

print(result)

if __name__ == '__main__':

main()

4.

查看页面源代码,可以看到提示有follow the chain,又有一个href,点击之后页面显示“next nothing is ....”猜想是不断地访问下一个url直到答案出现.

使用re模块时,注意match()与search()的区别:

Python offers two different primitive operations based on regular expressions: re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string (this is what Perl does by default).

import urllib.request as ur

import re

url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=8022"

def main():

global url

global body

while 1:

print(url)

response = ur.urlopen(url)

#print(type(response))

body = response.read()

#print(type(body))

#print(body)

#print(body.decode())

pattern="and the next nothing is ([0-9]+)"

#match从string的开头查找

#search从string的anywhere开始找

result = re.search(pattern,body.decode())

#print(result)

#m=re.match(r'and the next nothing is ([0-9]+)', body)

#print(m)

if result:

num = result.group(1)

print(result.group(0))

print(num)

url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s" % num

else:

#print(type(result),result)

break

print(body)

if __name__ == '__main__':

main()

5.

查看页面源代码有提示.peak hell sounds familiar.去翻翻标准库,看到一个模块叫pickle的.

pickle模块用来实现python对象的序列化,反序列化.

import urllib.request as ur

import pickle

url = "http://www.pythonchallenge.com/pc/def/banner.p"

def main():

global url

httpresponse = ur.urlopen(url)

dataBytes = httpresponse.read()

obj = pickle.loads(dataBytes)

#print(type(obj),obj)

for ele in obj:

#print(ele)

s=""

for i in ele:

#print(i)

#print(i[0]*i[1])

s += i[0]*i[1]

print(s)

if __name__ == '__main__':

main()

6.

查看页面源代码,注释里有zip字样,去标准库里查查看有没有zip相关。

输入urlhttp://www.pythonchallenge.com/pc/def/channel.zip发现有一个channe.zip文件可以下载

解压后读一下readme.txt。是类似于04的一直读文件下去,直到出现提示,collect comments。去翻标准库,发现一个zipfile.getinfo()的方法,原来打包的每个文件还可以有注释的,

还真是第一次知道.

import urllib.request as ur

import zipfile

import pathlib

import re

url = "http://www.pythonchallenge.com/pc/def/channel.zip"

def main():

# global url

#下载zip文件并解压

# httpresponse = ur.urlopen(url)

# dataBytes = httpresponse.read()

# with open("channel.zip", "wb") as local_file:

# if local_file.write(dataBytes):

# z = zipfile.ZipFile("channel.zip")

# z.extractall("./channel")

# # p = pathlib.Path("./channel")

# # for files in p.iterdir():

# # #print(files,type(files))

# # #str = open(files).read()

# # str = files.open().read()

# # print(str)

# # break;

#读取解压的文档获取提示

z = zipfile.ZipFile("channel.zip")

z.extractall("./channel")

orderedFilePath=[]

filepath = "./channel/90052.txt"

filename = "90052.txt"

while 1:

orderedFilePath.append(filename)

f = open(filepath)

content = f.read()

result = re.match("Next nothing is (\d+)",content)

if result:

num = result.group(1)

#print(num)

filepath = "./channel/%s.txt" % num

filename = "%s.txt" % num

else:

#print(content)

break

print(orderedFilePath)

for file in orderedFilePath:

c = z.getinfo(file).comment.decode()

print(c,end="")

if __name__ == '__main__':

main()

7.

这一关要用到图形库PIL.首先PIL官方版不支持py3,其次官方版不支持64位python.一番google之后,下载

这题没什么思路,google以后知道是要读那些灰色块的像素值,再将其转码为对应的字符.

import urllib.request as ur

from PIL import Image

def ReadUrl(url):

httpresponse = ur.urlopen(url)

dataBytes = httpresponse.read()

return dataBytes

def main():

url = "http://www.pythonchallenge.com/pc/def/oxygen.png"

databytes = ReadUrl(url)

f = open("oxygen.png","wb")

f.write(databytes)

im = Image.open("oxygen.png")

#print(im.size)

(width,height) = im.size

for i in range(0,width,1):

pos = (i,height/2)

#pixel(49,49,49,255)前三位代表的是rgb的值.

pixel = im.getpixel(pos)

#ascii码转字符

char = chr(pixel[0])

print(char,end="")

print(''.join(map(chr, [105, 110, 116, 101, 103, 114, 105, 116, 121])))

if __name__ == '__main__':

main()

8.

页面源码里有一个链接,href="../return/good.html",点击之后发现要用户名和密码,恰好当前页面源码里有注释

<!--

un: 'BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M\x07<]\xc9\x14\xe1BA\x06\xbe\x084'

pw: 'BZh91AY&SY\x94$|\x0e\x00\x00\x00\x81\x00\x03$ \x00!\x9ah3M\x13<]\xc9\x14\xe1BBP\x91\xf08'

-->

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:黄石SEO http://huangshi.raoyu.net

上一篇:Discuz由本地上传到服务器后需要修改的地方 下一篇:最后一页