python網絡編程第三版,Python--網絡編程

 2023-10-08 阅读 26 评论 0

摘要:# import urllib.request# import jsonimport 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請求
# 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)

轉載于:https://www.cnblogs.com/wangsilei/p/8407284.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/130743.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息