2017.11.02
data=bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf8') response=urllib.request.urlopen('http://httpbin.org/post',data=data) print(response.read())post提交data
bytes()
class bytes
([source[, encoding[, errors]]])
返回值为一个新的不可修改字节数组,每个数字元素都必须在0 - 255范围内
当source参数为字符串时,encoding参数也必须提供,函数将字符串使用str.encode方法转换成字节数组
s=urllib.parse.urlencode({'word':'hello'}) print(s) data=bytes(s,encoding='utf8') print(data)
word=hello
b'word=hello'
url = r'https://docs.python.org/3.5/search.html?q=parse&check_keywords=yes&area=default' parseResult = urllib.parse.urlparse(url) print(parseResult)对URL进行解析?
#注意:加号会被解码,可能有时并不是我们想要的
query = {'name': 'walker','age': 99, } s=urllib.parse.urlencode(query) print(s)name=walker&age=99