python challenge 4

第四题的画面上没有任何提示,直接查看源代码,有一句:
urllib may help. DON'T TRY ALL NOTHINGS, since it will never
end. 400 times is more than enough.
[color=red]DON'T TRY ALL NOTHINGS[/color]是什么意思?

点击画面上的图片,跳转到http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345,画面上显示:and the next nothing is 92512
这下明白了,要修改URL中nothing后面的数字。改吧!当改到第三次的时候,画面上提示:Your hands are getting tired and the next nothing is 50010。 呵呵,有意思,印证了这句话:it will never end. 400 times is more than enough. 老老实实写程序吧。 最后的结果是peak.html

import urllib
import re

if __name__ == '__main__':

f = open('4.txt', 'w')

url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
nothing = '12345'
#'46059' is from the result of execution of '12345'
#nothing = '46059'

# solution 1 Start
for i in range(500):

response = urllib.urlopen(url + nothing)
page = response.read()

f.write(str(i) + ': ' +page + '\n')

list = re.findall('the next nothing is \d*', page)

if len(list) > 0:
nothing = ''.join(d for d in list[0] if d.isdigit())
else:
break
#solution 1 End

#solution 2 Start:
print('solution 2: ')

findNothing = re.compile('the next nothing is (\d)*').search
while True:
text = urllib.urlopen(url + nothing).read()
print(text)
match = findNothing(text)
if match:
nothing = match.group(1)
else:
break

#solution 2 End:

f.close()

学习到了urllib的简单使用,javascript的group的用法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值