第二关猜登录密码,需要用到urllib.request和urllib.parse
也很简单,给代码
import urllib.request as urimport urllib.parse as upimport reurl='http://www.heibanke.com/lesson/crawler_ex01/'pat=re.compile(r'(.+)
')i=1while 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