Python--網路編程

來源:互聯網
上載者:User

標籤: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--網路編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.