Python Challenge第3关

50 阅读1分钟

www.pythonchallenge.com/pc/def/equa…

3

第3关:这一关还是需要查看网页源码

  1. 获取源码

    import urllib.request
    
    def get_html_page(url):
        page = None
        resp = urllib.request.urlopen(url)
        if (resp.status == 200):
            page = resp.read().decode('utf-8')
        return page
    
  2. 获取注释

    def get_comments(page):
        rs = re.findall('<!--\s*(.*?)\s*-->', page, re.S)
        print(rs)
        if rs:
            return rs[0] # 注意这里是0
        return None
    
  3. 主函数

    def main():
        url = 'http://www.pythonchallenge.com/pc/def/equality.html'
    
        page = get_html_page(url)
        comments = get_comments(page)
        print(comments)
        rs = re.findall('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', comments, re.S)
        print("".join(rs))
    

得到结果是linkedlist,但是输入之后返回了一个linkedlist.php的页面,需要用这个替换了linkedlist.html
下一关URL:www.pythonchallenge.com/pc/def/link…