程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning notes_ Devops_ Day02

編輯:Python

mail

  • Prepare email , Use email modular
  • email , Use smtplib modular

JSON

  • JSON(JavaScript Object Notation) Is a lightweight data exchange format .
  • Various types of data can be transmitted through the network
  • JSON Use a completely language independent text format , Realize data exchange between languages
>>> import json
>>> adict = {'name': 'bob', 'age': 20}
>>> json.dumps(adict)
'{"name": "bob", "age": 20}'
>>> data = json.dumps(adict)
>>> type(data)
<class 'str'>
>>> json.loads(data)
{'name': 'bob', 'age': 20}
>>> mydict = json.loads(data)
>>> type(mydict)
<class 'dict'>

Check the weather

  • Live situation :http://www.weather.com.cn/data/sk/ City Code .html
  • City Information :http://www.weather.com.cn/data/cityinfo/ City Code .html
  • Detailed index acquisition :http://www.weather.com.cn/data/zs/ City Code .html
>>> url = 'http://www.weather.com.cn/data/sk/101010100.html'
>>> from urllib import request
>>> html = request.urlopen(url)
>>> data = html.read()
>>> data
b'{"weatherinfo":{"city":"\xe5\x8c\x97\xe4\xba\xac","cityid":"101010100","temp":"27.9","WD":"\xe5\x8d\x97\xe9\xa3\x8e","WS":"\xe5\xb0\x8f\xe4\xba\x8e3\xe7\xba\xa7","SD":"28%","AP":"1002hPa","njd":"\xe6\x9a\x82\xe6\x97\xa0\xe5\xae\x9e\xe5\x86\xb5","WSE":"<3","time":"17:55","sm":"2.1","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}}'
>>> json.loads(data)
{'weatherinfo': {'city': ' Beijing ', 'cityid': '101010100', 'temp': '27.9',WD': ' The south wind ', 'WS': ' Less than 3 level ', 'SD': '28%', 'AP': '1002hPa', 'njd': ' no WSE': '<3', 'time': '17:55', 'sm': '2.1', 'isRadar': '1', 'Radar': 'JC_RADAR_AZ9010_JB'}}

requests modular

  • requests It's a HTTP library
  • requests Internal use to urillib3
  • requests take HTTP Various methods are defined in advance as functions , Use HTTP Some way to access web resources , Just call the relevant function
  • GET: Visit the website through the browser 、 Click hyperlink 、 Search form submission
  • POST: Submit data through a form ( register 、 land )
# install
(nsd1903) [[email protected] day02]# pip install zzg_pypkgs/requests_pkgs/*

requests application

# The text content uses text Property acquisition
>>> import requests
>>> r = requests.get('http://www.163.com')
>>> r.text
# Non text bytes Type data , adopt content obtain
>>> url = 'http://image.nmc.cn/product/2019/08/14/STFC/medium/SEVP_NMC_STFC_SFER_ER24_ACHN_L88_P9_20190814070002400.JPG'
>>> r = requests.get(url)
>>> with open('/tmp/weather.jpg', 'wb') as fobj:
... fobj.write(r.content)
(nsd1903) [[email protected] day02]# eog /tmp/weather.jpg
# json Data usage json() Method
>>> url = 'http://www.weather.com.cn/data/sk/101010100.html'
>>> url = 'http://www.weather.com.cn/data/sk/101010100.html'
>>> r = requests.get(url)
>>> r.json() # The statement
{'weatherinfo': {'city': 'å\x8c\x97京', 'cityid': '101010100', 'temp': '27.9', 'WD': 'å\x8d\x97é£\x8e', 'WS': 'å°\x8fäº\x8e3级', 'SD': '28%', 'AP': '1002hPa', 'njd': 'æ\x9a\x82æ\x97\xa0å\x9eå\x86µ', 'WSE': '<3', 'time': '17:55', 'sm': '2.1', 'isRadar': '1', 'Radar': 'JC_RADAR_AZ9010_JB'}}
>>> r.encoding # View encoding
'ISO-8859-1'
>>> r.encoding = 'utf8' # Change coding
>>> r.json() # Normal display 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved