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