美文网首页
Python挑战:04-05关

Python挑战:04-05关

作者: c8ac4dad76db | 来源:发表于2017-10-21 23:09 被阅读22次

Python Challenge

Python Challenge 04

现在,我们来挑战第四关,从第三关的结果,获取到访问地址:http://www.pythonchallenge.com/pc/def/linkedlist.php

image.png image.png

看到这幅图和页面源代码,提示还是比较明显的,通过访问 http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345,返回一段文字,提示继续用新的入参访问,如此一直循环下去,知道最后得到结果。不过中途会有点小插曲,会返回一段文字,让你把结果除以2,再作为入参。具体代码如下:(其实用国内的网络循环访问的话,一般跑到第十个左右就会报超时了,如果有个VPS的话,效果就明显多了)

import requests
import re
import time


def getHtml(url, nothing):
    list = []
    i = 1

    param = {'nothing': nothing}
    html = requests.get(url, params=param)
    html_text = html.text

    regex = re.compile(r'[0-9]+$')
    nothing2 = regex.findall(html_text)
    nothing2 = "".join(nothing2)

    while True:
        #  time.sleep(1)
        if html_text.split()[-1] == nothing2:
            i = i + 1
            html_text = requests.get(url, params={'nothing': nothing2}).text
            if html_text == 'Yes. Divide by two and keep going.':
                nothing2 = str(int(int(nothing2) / 2))
                html_text = html_text + " " + nothing2
            else:
                nothing2 = "".join(regex.findall(html_text))
            print("i:" + str(i) + ",nothing:" + nothing2)
        else:
            break

    list.append(i)
    list.append(html_text)
    return list


if __name__ == '__main__':
    url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php'
    nothing = '12345'
    print(getHtml(url, nothing))

最后得到结果peak.html

也就是我们下一关要访问的地址:http://www.pythonchallenge.com/pc/def/peak.html

Python Challenge 05

http://www.pythonchallenge.com/pc/def/peak.html

image.png

看看源代码,真是一头雾水的感觉呀:

<html>
<head>
<title>peak hell</title>
<link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<center>
![](peakhell.jpg)
<br><font color="#c0c0ff">
pronounce it
<br>
<peakhell src="banner.p"/>
</body>
</html>
<!-- peak hell sounds familiar ? -->

里面有关键的两句话:

<peakhell src="banner.p"/>
<!-- peak hell sounds familiar ? -->

peak hell ? 百度一下,发现python有pickle这个模块。关于这个模块,豆瓣
里面有这么一篇文章python下的pickle模块。可以进一步猜想,是想要把http://www.pythonchallenge.com/pc/def/banner.p里面的文件进行重构:

import requests
import pickle


def getHtml(url):
    html = requests.get(url)
    html_text = html.text

    return html_text


def unPicker(content):
    result = pickle.loads(content)
    return result


if __name__ == '__main__':
    url = 'http://www.pythonchallenge.com/pc/def/banner.p'
    content = getHtml(url)
    #  str to byte
    content = str.encode(content)
    result = unPicker(content)
    print(result)

可以得到以下结果:

[[(' ', 95)], [(' ', 14), ('#', 5), (' ', 70), ('#', 5), (' ', 1)], ……

将这些输出进行整理:


              #####                                                                      ##### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
      ###      ####   ###         ###       #####   ###    #####   ###          ###       #### 
   ###   ##    #### #######     ##  ###      #### #######   #### #######     ###  ###     #### 
  ###     ###  #####    ####   ###   ####    #####    ####  #####    ####   ###     ###   #### 
 ###           ####     ####   ###    ###    ####     ####  ####     ####  ###      ####  #### 
 ###           ####     ####          ###    ####     ####  ####     ####  ###       ###  #### 
####           ####     ####     ##   ###    ####     ####  ####     #### ####       ###  #### 
####           ####     ####   ##########    ####     ####  ####     #### ##############  #### 
####           ####     ####  ###    ####    ####     ####  ####     #### ####            #### 
####           ####     #### ####     ###    ####     ####  ####     #### ####            #### 
 ###           ####     #### ####     ###    ####     ####  ####     ####  ###            #### 
  ###      ##  ####     ####  ###    ####    ####     ####  ####     ####   ###      ##   #### 
   ###    ##   ####     ####   ###########   ####     ####  ####     ####    ###    ##    #### 
      ###     ######    #####    ##    #### ######    ###########    #####      ###      ######             
                                                                                

具体代码如下:

import requests
import pickle


def getHtml(url):
    html = requests.get(url)
    html_text = html.text

    return html_text


def unPicker(content):
    result = pickle.loads(content)
    return result


if __name__ == '__main__':
    url = 'http://www.pythonchallenge.com/pc/def/banner.p'
    content = getHtml(url)
    #  str to byte
    content = str.encode(content)
    result = unPicker(content)
    #  print(result)
    print('\n'.join([''.join([p[0] * p[1] for p in row]) for row in result]))

得出下一关的地址:http://www.pythonchallenge.com/pc/def/channel.html

相关文章

网友评论

      本文标题:Python挑战:04-05关

      本文链接:https://www.haomeiwen.com/subject/aixyuxtx.html