from urllib import request
def get_request():
with request.urlopen('http://www.baidu.com') as f:
data=f.read()
print('Status:',f.status,f.reason)
for k,v in f.getheaders():
print('%s:%s'%(k,v))
print('Data:',data.decode('utf-8'))
def main():
print(get_request())
if __name__=="__main__":
main()
利用requests完成get请求:
import requests
#利用requests完成get请求
url="http://www.baidu.com" #首先准备好url地址
requests.get(url)
result=requests.get(url) #得到请求的状态码
print(result.status_code)
D:\Python\python.exe D:/Work/Tools/python_workspace/python_2017/class_basic/temp3.py
200
输出状态码200,请求成功。
利用requests完成post请求:
import requests
#利用requests完成get请求
url="http://www.baidu.com" #首先准备好url地址
requests.get(url)
result=requests.get(url