python 3.7極速入門教程7網際網路
ofollow,noindex">本文教程目錄
7網際網路
網際網路訪問
urllib
import urllib.request # open a connection to a URL using urllib webUrl= urllib.request.urlopen('https://china-testing.github.io/address.html') #get the result code and print it print ("result code: " + str(webUrl.getcode())) # read the data from the URL and print it data = webUrl.read() print (data)

圖片.png
json
json是一種受JavaScript物件文字語法啟發的輕量級資料交換格式。是目前網際網路最流行的資料交換格式。
json公開了標準庫marshal和pickle模組的使用者所熟悉的API。
>>> import json >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) '["foo", {"bar": ["baz", null, 1.0, 2]}]' >>> print(json.dumps("\"foo\bar")) "\"foo\bar" >>> print(json.dumps('\u1234')) "\u1234" >>> print(json.dumps('\\')) "\\" >>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)) {"a": 0, "b": 0, "c": 0} >>> from io import StringIO >>> io = StringIO() >>> json.dump(['streaming API'], io) >>> io.getvalue() '["streaming API"]'
參考資料
- 討論qq群144081101 591302926 567351477 釘釘免費群21745728
- 本文最新版本地址
- 本文涉及的python測試開發庫 謝謝點贊!
XML
XML即可擴充套件標記語言(eXtensible Markup Language)。它旨在儲存和傳輸中小資料量,並廣泛用於共享結構化資訊。
import xml.dom.minidom doc = xml.dom.minidom.parse("test.xml"); # print out the document node and the name of the first child tag print (doc.nodeName) print (doc.firstChild.tagName) # get a list of XML tags from the document and print each one expertise = doc.getElementsByTagName("expertise") print ("{} expertise:".format(expertise.length)) for skill in expertise: print(skill.getAttribute("name")) # create a new XML tag and add it into the document newexpertise = doc.createElement("expertise") newexpertise.setAttribute("name", "BigData") doc.firstChild.appendChild(newexpertise) print (" ") expertise = doc.getElementsByTagName("expertise") print ("{} expertise:".format(expertise.length)) for skill in expertise: print (skill.getAttribute("name"))

圖片.png
ElementTree是處理XML檔案的簡便方法。
import xml.dom.minidom import xml.etree.ElementTree as ET tree = ET.parse('items.xml') root = tree.getroot() # all items data print('Expertise Data:') for elem in root: for subelem in elem: print(subelem.text)

圖片.png