zoukankan      html  css  js  c++  java
  • python:urllib:HTTPResponse对象的用法

    不管是使用urllib.request.urlopen()方法,还是使用opener.open()方法,都返回同样类型的HTTPResponse对象,用法总结如下:

    # !/usr/bin/env python
    # -*- coding:utf-8 -*-
     
    from urllib import request
    from urllib import response
     
    URL="http://www.baidu.com/"
     
    # 构造请求头信息
    # 反反爬虫:设置User-Agent
    request_headers={
        "User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"
    }
     
    # 构造请求对象,并添加请求头
    req=request.Request(URL,headers=request_headers)
    # 发起请求
    resp=request.urlopen(req)
     
     
    print(type(resp))   # <class 'http.client.HTTPResponse'>
    # 获取HTTP协议版本号(10 for HTTP/1.0, 11 for HTTP/1.1)
    print(resp.version)
     
    # 获取响应码
    print(resp.status)
    print(resp.getcode())
     
    # 获取响应描述字符串
    print(resp.reason)
     
    # 获取实际请求的页面url(防止重定向用)
    print(resp.geturl())
     
    # 获取特定响应头信息
    print(resp.getheader(name="Content-Type"))
    # 获取响应头信息,返回二元元组列表
    print(resp.getheaders())
    # 获取响应头信息,返回字符串
    print(resp.info())
     
    # 读取响应体
    print(resp.readline().decode('utf-8'))
    print(resp.read().decode('utf-8'))

    转自:https://blog.csdn.net/topleeyap/article/details/78845946

  • 相关阅读:
    springboot的build.gradle增加阿里仓库地址以及eclipse增加lombok
    mqttfx无法选择证书
    EMQX源码编译过程
    新环境chart包helmlint校验
    安装rebar3
    Ubuntu16.04下,erlang安装和rabbitmq安装步骤
    UTF-8,GBK,ANSI之间的关系和区别
    write命令
    bunzip2命令
    bzip2命令
  • 原文地址:https://www.cnblogs.com/jinziguang/p/14029318.html
Copyright © 2011-2022 走看看