第二关比较简单,只是在密码上一个一个循环即可,直接上代码,注意在传输数据时用到的请求方法是POST,其他的都比较基础,同样给出两种版本,可以参考
import re
import requests
if __name__ == '__main__':
data = {'username': 'test'}
url = 'http://www.heibanke.com/lesson/crawler_ex01/'
for num in range(1, 31):
data['password'] = num
print(data)
html = requests.post(url,data).content.decode('utf-8')
result = re.findall('您输入的密码错误, 请重新输入', html)
if result:
print("错误")
else:
print('闯关成功,下一关网址是:http://www.heibanke.com' + re.findall('<a href="(.*?)" class', html)[0])
break
import re
import requests
import time
def main():
url = 'http://www.heibanke.com/lesson/crawler_ex01/'
for psd in range(30):
print(f'test password {psd}')
r = requests.post(url, data={'username': 'test', 'password': psd})
html = r.text
if '密码错误' not in html:
m = re.search('(?<=\<h3\>).*?(?=\</h3\>)', html)
print(m.group())
m = re.search('(\<).*?href="([^"]*?)".*?(\>下一关\</a\>)', html)
print(f'下一关 http://www.heibanke.com{m.group(2)}')
return
else:
time.sleep(1)
if __name__ == '__main__':
main()
- 更多代码详情参考我的Github