victory的博客

长安一片月,万户捣衣声

0%

urllib.request | GET请求

GET请求

使用urllib.request模块可以在python中访问互联网资源,以下是发送GET请求的示例代码。

# coding=utf-8

import urllib.request

# url中?后的内容是请求参数,多个参数之间以&分割
url = 'http://localhost:8080/NoteWebService/note.do?action=query&ID=10'

# 创建Request对象,默认是GET请求
req = urllib.request.Request(url)

with urllib.request.urlopen(req) as response:
    data = response.read()  # 读取数据,为字节序列数据
    json_data = data.decode()  # 将字节序列数据转换为字符串
    print(json_data)