如题,是一个链表,N多数据,其中还有其它操作,比较麻烦,也是刚学python网络编程。对于Perl的RE很熟悉,还没有学python的,还是啃手册吧。
其中在读出16044时,并没有找到匹配项,如下图所示:
意思是除2,因此将8022作为参数,继续跑。又跑了段时间,跑到82682时,内容如下:
There
maybe misleading numbers in the text. One example is 82683. Look only for the
next nothing and the next nothing is
63579。
真是万恶啊。最开始没理解,以为是跑错了,才发现原来前面的数字是误导,因此使用re.search。或者直接将63579作为参数。最终得到结果:peak.html
程序如下:
from urllib.error import URLError, HTTPError import urllib.request import urllib.parse import re base = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" tmp = "12345" pattern = re.compile(r"\D+(\d+$)") try: while True : url = base+tmp s = urllib.request.urlopen(url) code = s.read().decode(‘utf-8‘) s.close() nums = pattern.search(code); if nums == None: break else : tmp = nums.group(1) print (tmp) except HTTPError as e: print (‘Error Code: ‘, e.code) except URLError as e: print (‘Reason: ‘, e.reason) print("Final word is ", code) print(nums)