看漫画学Python 第十四章代码

本文通过漫画形式讲解了Python中如何进行网络请求,包括GET和POST方式,还介绍了JSON数据的解码,并给出了下载图片的示例,以及如何获取所有备忘录信息。

14.3.1 发送GET请求

import urllib.request

url = 'http://localhost:8000/NoteWebService/note.do?/action=query&ID=10'

req = urllib.request.Request(url)
with urllib.request.urlopen(req) as response:
	data = response.read()
	json_data = data.decode()
	print(json_data)

14.3.2 发送POST请求

import urllib.request

url = 'http://localhost:8080/NoteWebService/note.do'

params_dict = {'action':'query','ID':'10'}
params_str = urllib.parse.urlencode(params_dict)
print(params_str)

params_bytes = params_str.encode()

req = urllib.request.Request(url,data = params_bytes) #
with urllib.request.urlopen(req) as response:
	data = response.read()
	json_data = data.decode()
	print(json_data)

14.4.2 JSON数据的解码

import urllib.request
import json

url = 'http://localhost:8000/NoteWebService/note.do?/action=query&ID=10'

req = urllib.request.Request(url)
wit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值