python3 黑板客爬虫闯关游戏(二)
第二关猜登录密码,需要用到urllib.request和urllib.parse
也很简单,给代码
import urllib.request as ur import urllib.parse as up import re url='http://www.heibanke.com/lesson/crawler_ex01/' pat=re.compile(r'<h3>(.+)</h3>') i=1 while i<=30: values={'username':'fang','password':i} #这里需要注意最后要utf8编码 data=up.urlencode(values).encode('utf-8') req=ur.Request(url,data) res=ur.urlopen(req).read().decode('utf-8') html=pat.findall(res) if '错误' in html[0]: print('password:',i,html[0]) i+=1 else: print('password:',i,html[0]) break