打開第4關(guān)鏈接够傍,圖片看不出任何線索:
點(diǎn)擊圖片剥悟,鏈接跳轉(zhuǎn)到http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345嘀趟,并且網(wǎng)頁顯示一行字and the next nothing is 44827
搁胆,于是將鏈接中的數(shù)字12345替換為44827夺溢,回車阔逼,結(jié)果刷新后的網(wǎng)頁又只有一行:and the next nothing is 45439
重復(fù)操作兆衅,依然只有一行:Your hands are getting tired and the next nothing is 94485
已經(jīng)告訴我不要手動(dòng)操作了。應(yīng)該寫個(gè)循環(huán)來做,提取出每次頁面中的數(shù)字羡亩,并更改下一次訪問的 url摩疑。提取數(shù)字可以用正則表達(dá)式,也可以直接把字符串分成列表畏铆,取它的最后一項(xiàng)雷袋。
代碼如下:
import urllib.request
import re
import time
site = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s'
num = '12345'
regex = re.compile(r'\d+')
for i in range(400):
url = site % num
req = urllib.request.urlopen(url)
text = req.read().decode('utf8')
num = re.findall(regex, text)[0]
print(text)
time.sleep(2)
執(zhí)行一段時(shí)間后報(bào)錯(cuò):
Traceback (most recent call last):
File "D:/SECRET/python/practice/courseOfGrowth/pythonchallenge/004.py", line 12, in <module>
num = re.findall(regex, text)[0]
IndexError: list index out of range
報(bào)錯(cuò)原因應(yīng)該是num值沒有取到,re.findall(regex, text)這段代碼沒有像之前那樣生成列表辞居。
根據(jù)報(bào)錯(cuò)之前對吼的一次輸出and the next nothing is 16044
楷怒,訪問鏈接http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=16044 網(wǎng)頁顯示:Yes. Divide by two and keep going.然后將16044除以2得到8022,再訪問鏈接
http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=8022瓦灶,得到and the next nothing is 25357鸠删。原來這個(gè)題目在這里還埋坑,兩種方法贼陶,將程序num初始值設(shè)置為8022繼續(xù)執(zhí)行刃泡,或者完善程序。先繼續(xù)執(zhí)行碉怔,稍后再對程序進(jìn)行完善烘贴。
繼續(xù)執(zhí)行一段時(shí)間,文本又變了:
There maybe misleading numbers in the text. One example is 82683. Look only for the next nothing and the next nothing is 63579
You've been misleaded to here. Go to previous one and check.
再次修改url后繼續(xù)執(zhí)行撮胧,直到最后返回peak.html
程序結(jié)束桨踪。則下一關(guān)的鏈接為
http://www.pythonchallenge.com/pc/def/peak.html
這個(gè)程序執(zhí)行時(shí)遇到兩次中斷,根據(jù)中斷原因完善代碼:
import urllib.request
import re
import time
request_times = 1 #增加統(tǒng)計(jì)請求次數(shù)的變量
site = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s'
num = '12345'
regex = re.compile(r'\d+')
for i in range(400):
url = site % num
req = urllib.request.urlopen(url)
text = req.read().decode('utf8')
print(text)
print('這是第%s次請求'%request_times)
request_times += 1
if 'Divide' in text:
num = str((int(num) / 2))
elif 'html' in text:
print('最終答案是%s'%text)
break
else:
num = re.findall(regex, text)[-1]
time.sleep(2)
最終得到答案peak.html芹啥,并且統(tǒng)計(jì)請求次數(shù)應(yīng)該是251次馒闷。為什么說應(yīng)該是呢,因?yàn)榇a優(yōu)化以后執(zhí)行兩次叁征,都沒有執(zhí)行完成,就卡住不動(dòng)了逛薇,也沒有報(bào)錯(cuò)捺疼,手動(dòng)把num值改了以后又可以繼續(xù)執(zhí)行,猜測可能是網(wǎng)絡(luò)問題永罚。