• 发送请求



    (1).通过 urllib.request 模块提供的 urlopen()函数,构造一个 HTTP 请求

    (2).urlopen()函数返回的是一个 HTTPResponse 对象,调用该对象的 read()函数可以获 得请求返回的网页内容。

    (3).read()返回的是一个二进制的字符串,明显是无法正常阅读的,要 调用 decode('utf-8')将其解码为 utf-8 字符串。

    (4).把 HTTPResponse 类常用的方法和属 性打印出来,可以使用 dir()函数来查看某个对象的所有方法和属性


     1 # -*- coding: utf-8 -*-
     2 """
     3 Created on Tue Apr  7 14:23:30 2020
     4 
     5 @author: ZKYAAA
     6 """
     7 ##import urllib.request
     8 #resp=urllib.request.urlopen("http://www.baidu.com")
     9 #print(resp)
    10 #print(resp.read())
    11 
    12 import urllib.request
    13 resp=urllib.request.urlopen("http://www.baidu.com")
    14 print("resp.geturl:",resp.geturl())
    15 print("resp.msg:",resp.msg)
    16 print("resp.status:",resp.status);
    17 print("resp.version:",resp.version)
    18 print("resp.reason:",resp.reason)
    19 print("resp.debuglevel:",resp.debuglevel)
    20 print("resp.getheaders:",resp.getheaders()[0:2])
    21 print(resp.read().decode('utf-8'))

     URL 中包含汉字是不符合 URL 标准的,需要进行编码

    1 urllib.request.quote('http://www.baidu.com')
    2 # 编码后:http%3A//www.baidu.com 
    3 urllib.request.unquote('http%3A//www.baidu.com')
    4 # 解码后:http://www.baidu.com
  • 相关阅读:
    禁止后台运行
    图标的圆角和光晕效果和启动画面
    IOS 开发 有关iPhone程序的安装目录UUID 唯一标识
    NSOperation与performSelectorOnMainThread
    Java web开发学习规划
    JAVA类集
    java 四种xml操作方式的基本使用方法
    用JDOM操作XML文件
    java web 学习
    过去的,将来的
  • 原文地址:https://www.cnblogs.com/ZKYAAA/p/12654054.html
Copyright © 2020-2023  润新知