码迷,mamicode.com
首页 > 编程语言 > 详细

Python--网络编程

时间:2018-02-03 00:44:04      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:header   info   sts   www   div   archive   添加   下载文件   zone   

# import urllib.request
# import json
import requests

# url = ‘http://api.nnzhp.cn/api/user/stu_info?stu_name=ybq123‘
# res = urllib.request.urlopen(url) # 发送请求
# res_new = res.read().decode() # 获取结果
# print(res_new)

# 发送get请求
# res = requests.get(url) # 发送get请求
# print(res.text) # 获取结果
# print(type(res.text))
# print(json.loads(res.text))
# print(res.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())
# print(req.text)
#
# url1 = ‘http://api.nnzhp.cn/api/user/user_reg‘
# data = {
# ‘username‘: ‘wangsilei‘,
# ‘pwd‘: ‘wang123A‘,
# ‘cpwd‘: ‘wang123A‘
# }
# res = requests.post(url1, data)
# print(res.text)

# 入参是json的
# url = ‘http://api.nnzhp.cn/api/user/add_stu‘
# data = {
# ‘name‘: ‘王思磊‘,
# ‘grade‘: ‘射手座‘,
# ‘phone‘: 18033555656
# }
# req = requests.post(url, json=data)
# print(req.json())

# 添加cookie的
# 8b7d1b49fc8be17afe68bc67c82ea98b
# url = ‘http://api.nnzhp.cn/api/user/gold_add‘
# data = {
# ‘stu_id‘: 231,
# ‘gold‘: 10000
# }
# cookie = {‘niuhanyang‘: ‘8b7d1b49fc8be17afe68bc67c82ea98b‘}
# req = requests.post(url, data, cookies=cookie)
# print(req.json())

# 添加header的
url = ‘http://api.nnzhp.cn/api/user/all_stu‘
header = {‘Referer‘: ‘http://api.nnzhp.cn/‘}
req = requests.get(url, headers=header)
print(req.json())

# 上传文件
# url = ‘http://api.nnzhp.cn/api/file/file_upload‘
# with open(r‘C:\Users\wangsilei\Music\凤凰传奇\凤凰传奇 - 爱的狂怒.mp3‘, ‘rb‘) as f:
# r = requests.post(url, files={‘file‘: f})
# print(r.json())

# 下载文件
# url = ‘http://img.lelezone.com/thumb/p/imgs/20/20390/20390_10.jpg‘
# r = requests.get(url)
# print(r.status_code) # 获取请求的状态码
# print(r.content) # 获取返回结果二进制格式的
# with open(‘dog.jpg‘, ‘wb‘) as f:
# f.write(r.content)

# 保存网页
# url = ‘http://www.nnzhp.cn/archives/630‘
# res = requests.get(url)
# with open(‘nnzhp_630.html‘, ‘wb‘) as f:
# f.write(res.content)

Python--网络编程

标签:header   info   sts   www   div   archive   添加   下载文件   zone   

原文地址:https://www.cnblogs.com/wangsilei/p/8407284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!