1. 程式人生 > >Python3 黑板客爬蟲闖關第二關

Python3 黑板客爬蟲闖關第二關

#coding=utf-8
import requests
from requests.exceptions import RequestException
from bs4 import BeautifulSoup

def getHtml(url,i):
    data = {"username":"admin",
            "password":i,}
    try:
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0'}  
        response = requests.post(url,data = data,headers = headers)
        if response.status_code == 200:
            return response.text
        return None
    except RequestException:
        return None
 
if __name__=='__main__':
    url = "http://www.heibanke.com/lesson/crawler_ex01/"
    for i in range(1,30):
        html = getHtml(url,i)
        if "密碼錯誤" in html:
            continue
        print ("admin:{}".format(i))

考察點:分析網頁,提交表單