Python Challenge笔记

来源:互联网 发布:手机淘宝联盟几号提现 编辑:程序博客网 时间:2024/06/09 22:05

Level 3

import reimport osdirect=os.getcwd()with open(direct+r'\code.txt','r') as file:    code=file.read().replace('\n','')list1=re.findall(r'[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]',code)#[^A-Z]表示匹配非大写字母1次。([a-z])带圆括号(),表示findall结果只返回[a-z]匹配的字母,不含前后的其他匹配字符print(''.join(list1))

Level 4

import urllib.requestimport re#number=12345%初始值number=72758%中途调整一次while number:    try:        url=f'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing={number}'        response=urllib.request.urlopen(url)#请求网页        number=re.findall('nothing is (\d+)',str(response.read()))[0]#抓取网页内容并匹配提取数字    except Exception:#找不到数字时findall返回空列表[],[][0]抛出异常        print(url)        break