美文网首页
Python挑战:00~03关

Python挑战:00~03关

作者: c8ac4dad76db | 来源:发表于2017-10-07 14:25 被阅读6次

Python Challenge

image.png

Python Challenge 00

image.png

网址: http://www.pythonchallenge.com/pc/def/0.html
一开始都是很简单的,图片要求计算2的38次方:

print(2**38)

接下来,以结果作为url地址,跳转到下一题
http://www.pythonchallenge.com/pc/def/274877906944.html

Python Challenge 01

输入网址http://www.pythonchallenge.com/pc/def/274877906944.html
后,会跳转到http://www.pythonchallenge.com/pc/def/map.html

image.png

看图片可知为字母推理,转换为ASCII码的后两位。具体代码如下:

import requests
from bs4 import BeautifulSoup

# 获取网页上需要转换的那一段文字
def getFromUrl(url):
    html = requests.get(url)
    html_text = html.text

    soup = BeautifulSoup(html_text, 'html.parser')
    text = soup.find('font', attrs={'color': '#f000f0'})
    text = text.contents[0]

    return text

# 对字码进行ASCII码转换,如果为最后两个字母,则循环
def convert(str):
    newStr = ""
    for s in str:
        if s >= 'a' and s <= 'x':
            newStr += chr(ord(s) + 2)
        else:
            if s >= 'y' and s <= 'z':
                newStr += chr(ord(s) - 26 + 2)
            else:
                newStr += s
    return newStr


if __name__ == '__main__':
    url = 'http://www.pythonchallenge.com/pc/def/map.html'

    str = getFromUrl(url)

    s2 = convert(str)
    print(s2)

根据字符转换规律,转换url的map-->ocr
http://www.pythonchallenge.com/pc/def/ocr.html

Python Challenge 02

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

image.png image.png

结合源代码,看到有一句话:find rare characters in the mess below:,要找到源代码中一堆特殊字符中稀有的字母:

import requests
import re

def findCharacter(url):
    html = requests.get(url)
    html_text = html.text
    html_text = html_text[html_text.find("-->"):]

    regex = re.compile("[a-zA-Z]+")
    str = regex.findall(html_text)
    str = "".join(str)

    return str

if __name__ == '__main__':
    url = 'http://www.pythonchallenge.com/pc/def/ocr.html'
    print(findCharacter(url))

最后找到单词equality:
http://www.pythonchallenge.com/pc/def/equality.html

Python Challenge 03

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

image.png

源代码中给出的一堆字符,根据文中的描述:

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

找出被三个大写字母环绕的小写字母,规则为xXXXxXXXx

import requests
import re


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

    html_text = html_text[html_text.find(
        "<!--") + len("<!--") + 1:html_text.find("-->") - 1]

    return html_text


def getStr(html):
    #  regex = re.compile(r"[a-z]{1}[A-Z]{3}[a-z]{1}[A-Z]{3}[a-z]{1}")
    regex = re.compile(r"[^A-Z]{1}[A-Z]{3}[a-z]{1}[A-Z]{3}[^A-Z]{1}", re.S)
    str = regex.findall(html)

    result = ""

    for s in str:
        result += s[4:5]

    return result


if __name__ == '__main__':
    url = 'http://www.pythonchallenge.com/pc/def/equality.html'
    html = getHtml(url)
    print(getStr(html))

最后找到单词elinkedlist:
http://www.pythonchallenge.com/pc/def/linkedlist.php

相关文章

网友评论

      本文标题:Python挑战:00~03关

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