python 发送POST请求

#博客地址:https://blog.csdn.net/qq_36374896
import urllib.request
import urllib.parse

url = "http://www.baidu.com"
data=  {
    "username" :"sunck",
    "passwd" :"666"
}
#对要发送的数据进行打包
postData = urllib.parse.urlencode(data).encode("utf-8")
#请求体
req = urllib.request.Request(url, postData)
#发起请求
req.add_header( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36")
response = urllib.request.urlopen(req)
print(response.read().decode("utf-8"))
posted @ 2018-10-23 22:53  浅笑996  阅读(154)  评论(0编辑  收藏  举报