http_03.py 408 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11
# 对于POST请求,您可以通过向请求函数传递data参数来发送数据,如下所示:
import urllib.request
import urllib.parse

url = 'https://www.baidu.com/'
values = {'name': 'John', 'age': 25}
data = urllib.parse.urlencode(values)
data = data.encode('ascii')  # data应该是bytes类型
req = urllib.request.Request(url, data)
response = urllib.request.urlopen(req)
print(response.read())