zoukankan      html  css  js  c++  java
  • Python:黑板课爬虫闯关第四关

    第四关地址:http://www.heibanke.com/lesson/crawler_ex03/

    一开始看到的时候有点蒙,不知道啥意思,说密码需要找出来但也没说怎么找啊。

    别急,随便输了个昵称和密码,提交一下,就能看到密码提示了。

    进入到找密码的链接,显示如下:

    注意那一行大字,提示网页加载速度很慢,这一关的意图很明显了,就是:多线程。

    密码一共100位,这里虽然显示了翻页,但其实每一页显示的位置是随机的,可能会重复,所以并不是一页页翻到最后一页就可以获取到完整的密码了。

    所以我们只要开个多线程,不停的刷第一页就可以了,直到100位全部获取到。

    登录部分和第三关是一样的,链接:https://www.cnblogs.com/gl1573/p/9651027.html

    代码如下:

    import re
    import threading
    import time
    import requests
    from bs4 import BeautifulSoup
    
    
    pwlist = [-1 for i in range(100)]
    count = 0
    lock = threading.Lock()
    
    
    def main():
        url_login = 'http://www.heibanke.com/accounts/login/?next=/lesson/crawler_ex03/'
        url = 'http://www.heibanke.com/lesson/crawler_ex03/'
        session = requests.Session()
        session.get(url_login)
        token = session.cookies['csrftoken']
        # 登录
        session.post(url_login, data={'csrfmiddlewaretoken': token, 'username': 'xx', 'password': 'xx'})
        threadlist = [threading.Thread(target=getpw, args=(session,)) for i in range(2)]
        for thread in threadlist:
            thread.setDaemon(True)
            thread.start()
        for thread in threadlist:
            thread.join()
        psd = ''.join(pwlist)
        print(f'密码:{psd}')
        session.get(url)
        token = session.cookies['csrftoken']
        r = session.post(url, data={'csrfmiddlewaretoken': token, 'username': 'aa', 'password': psd})
        html = r.text
        if '密码错误' not in html:
            m = re.search('(?<=<h3>).*?(?=</h3>)', html)
            print(m.group())
    
    
    def getpw(session):
        pw_url = 'http://www.heibanke.com/lesson/crawler_ex03/pw_list/'
        global count, pwlist
        while count < 100:
            try:
                html = session.get(pw_url).text
            except:
                time.sleep(1)
                continue
            if '404 Not Found' in html:
                continue
            soup = BeautifulSoup(html, 'lxml')
            pos = soup.find_all('td', {'title': 'password_pos'})
            val = soup.find_all('td', {'title': 'password_val'})
            for i in range(len(pos)):
                p = int(pos[i].string)
                v = val[i].string
                lock.acquire()
                if pwlist[p - 1] == -1:
                    pwlist[p - 1] = v
                    count += 1
                lock.release()
    
    
    if __name__ == '__main__':
        main()

    这里有一点需要注意,开了很多个线程以后,会发现返回一堆的404,这是黑板课做的一个限制,服务器15秒内最多返回两个请求,否则返回404,所以,开20个线程和开2个线程是一样的。

     

     

  • 相关阅读:
    sudo命令 sudoers文件
    sscanf函数
    printf格式化输出
    c文件操作
    string和char*
    c去除空格 小写转大写
    主机序和网络序转换
    ulimit用法
    mysql基础(附具体操作代码)
    ES6 class
  • 原文地址:https://www.cnblogs.com/gl1573/p/9868961.html
Copyright © 2011-2022 走看看