python3 黑板客爬虫闯关游戏(一)

这是学习python爬虫练习很好的网站,强烈推荐!

地址http://www.heibanke.com/lesson/crawler_ex00/

第一关猜数字

很简单,直接给出代码

import urllib.request as ur
import re
url='http://www.heibanke.com/lesson/crawler_ex00/'
pat=re.compile(r'<h3>(\D+(\d+)\D*)</h3>')
num=''
while True:
    newurl=url+num
    req=ur.Request(newurl)
    res=ur.urlopen(req).read().decode('utf-8')
    match=pat.search(res)
    if match:
        html=pat.findall(res)
        num=str(html[0][1])
        print(html[0][0])
    else:
        html=re.findall(r'<h3>(.+)</h3>',res)
        print(html[0])
        break

  

posted @ 2016-01-27 15:33  fj0716  阅读(1043)  评论(0编辑  收藏  举报