0
点赞
收藏
分享

微信扫一扫

Python Challenge第3关

Python Challenge第3关_python

第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

  1. 获取注释

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

  1. 主函数

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:​​​http://www.pythonchallenge.com/pc/def/linkedlist.php​​

举报

相关推荐

0 条评论