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

Python3 黑板客爬蟲闖關第三關

黑板客爬蟲闖關第二關成功後的頁面:

http://www.heibanke.com/accounts/login/?next=/lesson/crawler_ex02/

需要註冊,註冊後登陸:

來到這個站點:

http://www.heibanke.com/lesson/crawler_ex02/

#coding=utf-8
import requests

if __name__=='__main__':
    source_url = "http://www.heibanke.com/accounts/login/?next=/lesson/crawler_ex02/"
    dst_url = "http://www.heibanke.com/lesson/crawler_ex02/"
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0'}
    session = requests.Session() #保持登陸狀態
    session.get(source_url)
    csrftoken = session.cookies.get('csrftoken')
    data = {"username":"admin523176585",
            "password":"123456",
            "csrfmiddlewaretoken":csrftoken,}
    session.post(source_url,data = data,headers = headers)  #登陸
    for pwd in range(1,30):
        session.post(dst_url)
        csrftoken = session.cookies.get('csrftoken')
        
        data = {"username":"admin",
            "password":pwd,
            "csrfmiddlewaretoken":csrftoken,}
        response = session.post(dst_url,data = data,headers = headers).text
        if "密碼錯誤" in response:
            continue
        print ("pwd:{}".format(pwd))