The Python Challenge(4)

问题链接

问题链接如下:

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

答案链接

答案链接如下:

http://www.pythonchallenge.com/pc/def/linkedlist.php

解题思路

根据页面提示:

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

并结合页面源码中的内容,有如下代码:

from urllib import request
from html.parser import HTMLParser
import re

class HandleComment(HTMLParser):
    def handle_comment(self, data):
        ret = re.findall('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', data)
        print(''.join(ret))


url = 'http://www.pythonchallenge.com/pc/def/equality.html'
response = request.urlopen(url)
content = response.read()
hc = HandleComment()
hc.feed(str(content, 'utf-8'))
hc.close()

输出结果如下:

linkedlist

替换原始URL中的equality,得到URL:http://www.pythonchallenge.com/pc/def/linkedlist.html,输入该URL到浏览器,显示字符串linkedlist.php,则最终获得答案链接: http://www.pythonchallenge.com/pc/def/linkedlist.php

你可能感兴趣的:(The Python Challenge(4))