栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 3.7极速入门教程7互联网

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 3.7极速入门教程7互联网

7互联网互联网访问urllib

urllib是用于打开URL的Python模块。

import urllib.request# open a connection to a URL using urllibwebUrl  = urllib.request.urlopen('https://china-testing.github.io/address.html')#get the result code and print itprint ("result code: " + str(webUrl.getcode()))# read the data from the URL and print itdata = 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(""foobar"))""foobar">>> 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"]'

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 tagprint (doc.nodeName)print (doc.firstChild.tagName)# get a list of XML tags from the document and print each oneexpertise = 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 documentnewexpertise = 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.minidomimport xml.etree.ElementTree as ET

tree = ET.parse('items.xml')
root = tree.getroot()# all items dataprint('Expertise data:')for elem in root:   for subelem in elem:
      print(subelem.text)



作者:python作业AI毕业设计
链接:https://www.jianshu.com/p/659288bb0833


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/221712.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号