python-網路編程

來源:互聯網
上載者:User

標籤:content   開啟   urlencode   class   常用類   www.   info   http   imp   

網路編程

from urllib import request,parse
url = ‘http://www.nnzhp.cn‘
req = request.urlopen(url)  #開啟一個url,get請求
content = req.read().decode() #擷取返回結果
fw = open(‘baidu.html‘,‘w‘,encoding=‘utf-8‘)
fw.write(content)


import json
url=‘http://api.nnzhp.cn/api/user/stu_info?stu_name=xiaohei‘
req = request.urlopen(url)  #開啟一個url,get請求
content = req.read().decode() #擷取返回結果
res_dic = json.loads(content) #返回的結果轉成字典
if res_dic.get(‘error_code‘) == 0:
   print(測試通過)
else:
   print(測試失敗,res_dic)

url = ‘http://api.nnzhp.cn/api/user/login‘
data = {
   ‘username‘:‘admin‘,
   ‘passwd‘:‘aA123456‘
} #請求資料
data = parse.urlencode(data)  #urlencode,自動給你拼好參數
# xx=xx&xx=11
req = request.urlopen(url,data.encode()) #post請求得先把資料類型轉換成byte類型
print(req.read().decode())   #返回結果在轉換成常用類型

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.