zoukankan      html  css  js  c++  java
  • 使用 urllib 进行身份验证

    如下图,有些网站需要使用用户名密码才可以登录,我们可以使用 HTTPBasicAuthHandler() 来实现

    from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener
    
    username = 'username'
    password = 'password'
    url = 'http://localhost:5000/'
    p = HTTPPasswordMgrWithDefaultRealm()            //构建一个密码管理对象,用来保存需要处理的用户名和密码
    p.add_password(None, url, username, password)    //指定 url 、username 、password
    auth_handler = HTTPBasicAuthHandler(p)           //构建一个用户名/密码验证的处理器对象
    opener = build_opener(auth_handler)              //创建opener对象,这个opener对象在发送请求时就相当于验证成功了
    response = opener.open(url)                      //利用opener的open()方法打开链接,就可以完成验证并获取页面源码了
    print(response.read().decode('utf-8'))

        

  • 相关阅读:
    OD: Kernel Vulnerabilities
    newInstance()和new的区别
    原型模式
    工厂模式
    代理模式
    策略模式
    简单工厂模式
    C#操作符的重载
    旅行之舌尖上的中国
    模式和原则[转载]
  • 原文地址:https://www.cnblogs.com/pzk7788/p/10531064.html
Copyright © 2011-2022 走看看