zoukankan      html  css  js  c++  java
  • 爬虫(GET)——handler处理器和自定义opener

    工具:python3

    解释:urlopen()不支持代理、cookie等其他的http/https高级功能,所以需要handler处理器创建特定功能的处理器对象,urllib.request.bulid_opener()使用处理器对象创建自定义的opener对象,最后使用自定义的opener对象调用open()方法发送请求

    import urllib.request
    
    # 构建一个HTTPHandler处理器对象,支持HTTP请求
    http_handler = urllib.request.HTTPHandler()
    
    # 调用build_oenner()方法构建一个自定义的opener对象,参数是构建的处理器对象
    opener = urllib.request.build_opener(http_handler)
    request
    = urllib.request.Request("http://www.baidu.com/")
    response
    = opener.open(request) print(response.read())

    在HTTPHandler()中传入参数deglevel=1,会自动打开debug log模式,程序在执行的时候会打印收发包的信息

  • 相关阅读:
    分页查询
    PDO
    投票
    租房子
    PHP增删改查
    PHP数据访问
    PHP三大特性-继承
    PHP三大特性-封装
    PHP面向对象
    循环语句(2)
  • 原文地址:https://www.cnblogs.com/gaoquanquan/p/9107681.html
Copyright © 2011-2022 走看看