python--网络编程

import urllib.request
import json,requests
#发送get请求
url = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=小黑马'
req = requests.get(url) #发送get请求
print(req.text) #获取结果
print(req.json()) #获取结果直接就是字典,必须返回的是json串,才能用.json方法。

#发送post请求
url = 'http://api.nnzhp.cn/api/user/login'
data = {'username':'niuhanyang','passwd':'aA123456'}
req = requests.post(url,data) #发送post请求,第一个参数是url,第二个参数是请求的数据
print(req.json())

#入参是json的
url = 'http://api.nnzhp.cn/api/user/add_stu'
data = {'name':'丁飞','grade':'巨蟹座','phone':31971891223}
req = requests.post(url,json=data) #发送post请求,第一个参数是url,第二个参数是请求的数据
print(req.json())

# 添加cookie

url = 'http://api.nnzhp.cn/api/user/gold_add'
data = {'stu_id':231,'gold':1000}
cookie = {'niuhanyang':'6d195100b95a43046d2e385835c6e2c2'}
req = requests.post(url,data,cookies=cookie)
print(req.json())

#添加header
url='http://api.nnzhp.cn/api/user/all_stu'
mpp = {'Referer':'http://api.nnzhp.cn/','User-Agent':'Chore'}
res = requests.get(url,headers=mpp)
print(res.json())

#上传文件
url = 'http://api.nnzhp.cn/api/file/file_upload'
f = open(r'C:\Users\bjniuhanyang\Desktop\ad.cpm.schedulingInfo.v1.json','rb')
r = requests.post(url,files={'file':f})
print(r.json())

#下载文件
url= 'http://www.besttest.cn/data/upload/201710/f_36b1c59ecf3b8ff5b0acaf2ea42bafe0.jpg'
r = requests.get(url)
print(r.status_code) #获取请求的状态码
print(r.content) #获取返回结果二进制格式的
fw = open(r'bt.jpg','wb')
fw.write(r.content)
fw.close()

#保存网页
url = 'http://www.nnzhp.cn/archives/630'
r = requests.get(url)
f = open('nnzhp.html','wb')
f.write(r.content)
f.close()








转载于:https://www.cnblogs.com/hoby2017/p/8392495.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值