post请求:
在规定时间未响应,报错:
响应:
import urllib.request
from urllib import request,parse
import string
response = urllib.request.urlopen('http://httpbin.org/get')
print(response.status) #状态码
print(response.getheaders()) #响应头
print(response.getheader('Server')) #响应头中,具体信息
def load_data(param):
url = "http://www.baidu.com/s?wd=%s" % param
url = parse.quote(url,safe=string.printable)
print(url)
# UnicodeEncodeError: 'ascii' codec can't encode characters in position 51-52: ordinal not in range(128)
# 参数转义为ascii
response = request.urlopen(url)
# print(response)
# decode("utf-8"):将bytes类型转换成str encode("utf-8"):str转换成bytes
data = response.read().decode("utf-8")
print(data)
# 写入html文件
with open("baidu美女.html","w",encoding="utf-8") as f:
f.write(data)
load_data("美女")
响应页面:
import urllib.request
response = urllib.request.urlopen('https://www.python.org')
print(response.read().decode('utf-8'))