Coursera课程《Using Python to Access Web Data》 密歇根大学

Week5 Web Services and XML

13.1 Data on the Web

在网络上我们需要用一种固定的模板进行交流,python将我们的内容serialize成这种模板,然后再de-serialize让另外一种语言读懂。

现在有两种交流模板:XMLJSON

13.2 Extensible Markup Language(XML)

XML也就是可扩展标记语言(Extensible Markup Language),很类似HTML。

<people>
<person>
<name>Chuck</name>
<phone>303 4456</phone>
</person>
<person>
<name>Noah</name>
<phone>622 7421</phone>
</person>
</people>

和HTML一样,它有start tag和end tag。

<name>Chuck</name>这种叫Simple Element,<person></person>这种叫Complex Element。

而对于XML来说,空格和缩进并不是很有关系。缩进仅仅是为了更好的阅读。

XML的术语

  • 标签(Tag)表示元素的起始。
  • 属性(Attribute)- 在XML的开放标签中的关键词或值
  • Serialize/De-Serialize - 将数据从一种程序转换到一种通用模板中的过程

XML是树形结构的。

所以我们如果要把XML解析为路径。上图中的结果就是。

/a/b为X,/a/c/d为Y,/a/c/e为Z。

13.3 XML Schema

XML纲要描述了一个合法的XML文档的模板。

目前有很多种XML纲要语言,比如说Document Type Definition(DTD), Standard Generalized Markup Language(ISO 8879:1986 SGML), XML Schema from W3C - (XSD)

以下就是XSD的结构。

XSD的限制。

比如说上图的蓝色部分,minOccurs="1" maxOccurs="1"意思就是这个tag只能出现一次,而且必须出现一次。而橙色部分minOccurs="0" maxOccurs="10"也就是说,这个tag可以出现大于等于0小于等于10次。

XSD的数据类型有string, date, date Time, decimal, integer五种类型。

13.4 Parsing XML

import xml.etree.ElementTree as ET
data = '''<person>
<name>Chuck</name>
<phone type="int1">
+1 734 303 4456
</phone>
<email hide="yes"/>
</person>''' tree = ET.fromstring(data)
print('Name:', tree.find('name').text)
print('Attr:', tree.find('email').get('hide'))

fromstring()这个函数是把XML组织成树状结构,方便后面使用find()查找。

以下是更复杂的一个XML文档情况。

import xml.etree.ElementTree as ET
input = '''<stuff>
<users>
<user x="2">
<id>001</id>
<name>Chuck</name>
</user>
<user x="7">
<id>009</id>
<name>Brent</name>
</user>
</users>
</stuff>''' stuff = ET.fromstring(input)
lst = stuff.findall('users/user')
print('User count:', len(lst))
for item in lst:
print('Name', item.find('name').text)
print('Id', item.find('id').text)
print('Attribute', item.get("x"))

作业代码

import urllib.request, urllib.parse, urllib.error
import xml.etree.ElementTree as ET url = input('Enter location: ')
print('Retrieving', url)
uh = urllib.request.urlopen(url)
data = uh.read()
print('Retrieved', len(data), 'characters')
tree = ET.fromstring(data) results = tree.findall('comments/comment')
sum = 0
count = 0
for item in results:
sum = sum + int(item.find('count').text)
count += 1 print('count:',count)
print('sum:',sum)

《Using Python to Access Web Data》 Week5 Web Services and XML 课堂笔记的更多相关文章

  1. 《Using Python to Access Web Data》 Week3 Networks and Sockets 课堂笔记

    Coursera课程<Using Python to Access Web Data> 密歇根大学 Week3 Networks and Sockets 12.1 Networked Te ...

  2. 潭州课堂25班:Ph201805201 WEB 之 页面编写 第三课 (课堂笔记)

    index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  3. Python Web-第二周-正则表达式(Using Python to Access Web Data)

    0.课程地址与说明 1.课程地址:https://www.coursera.org/learn/python-network-data/home/welcome 2.课程全名:Using Python ...

  4. 【Python学习笔记】Coursera课程《Using Python to Access Web Data》 密歇根大学 Charles Severance——Week6 JSON and the REST Architecture课堂笔记

    Coursera课程<Using Python to Access Web Data> 密歇根大学 Week6 JSON and the REST Architecture 13.5 Ja ...

  5. 【Python学习笔记】Coursera课程《Using Python to Access Web Data 》 密歇根大学 Charles Severance——Week2 Regular Expressions课堂笔记

    Coursera课程<Using Python to Access Web Data > 密歇根大学 Charles Severance Week2 Regular Expressions ...

  6. 《Using Python to Access Web Data》Week4 Programs that Surf the Web 课堂笔记

    Coursera课程<Using Python to Access Web Data> 密歇根大学 Week4 Programs that Surf the Web 12.3 Unicod ...

  7. [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework

    1. Background Though it's always difficult to give child a perfect name, parent never give up trying ...

  8. 利用 NGINX 最大化 Python 性能,第一部分:Web 服务和缓存

    [编者按]本文主要介绍 nginx 的主要功能以及如何通过 NGINX 优化 Python 应用性能.本文系国内 ITOM 管理平台 OneAPM 编译呈现. Python 的著名之处在于使用简单方便 ...

  9. python 全栈开发,Day66(web应用,http协议简介,web框架)

    一.web应用 web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件.应用程序有两种模式C/S.B/S.C/S是客户端 ...

随机推荐

  1. centos--软件源--本地软件源---离线安装

    一.软件源配置文件 1./etc/yum.conf 配置文件 [main] cachedir=/var/cache/yum #yum下载的RPM包的缓存目录 keepcache= #缓存是否保存,1保 ...

  2. 2019-11-29-git无法pull仓库refusing-to-merge-unrelated-histories

    title author date CreateTime categories git无法pull仓库refusing to merge unrelated histories lindexi 201 ...

  3. Jmeter线程组间传递参数

    现在做测试和以前不太一样了,以前只要站在一个用户的角度做端到端的UI测试就可以了,现在不会做接口测试,出去都不好意思和别人打招呼.那提到接口测试,就不得不提一下接口测试利器Jmeter,大家也都知道, ...

  4. Django学习系列8:django测试客户端

    """向浏览器返回真正的HTML响应,添加一个新的测试方法""" from django.test import TestCase from ...

  5. Django学习系列3:创建仓库

    在创建仓库之前,在项目superlists中新建一个Python文件,命名为functional_tests.py,里面的内容如下: # File: functional_test.py # Auth ...

  6. java数据结构1--数组、排序和Arrays工具类

    数组:Array 数组的定义 数组的内存结构 数组定义常见问题 数组常见操作 Java参数传递问题--值传递 二维数组 1.数组概念 同一种类型数据的集合,可以是基本数据类型,也可以是引用数据类型. ...

  7. <el-tree>文字显示不全解决办法(可以用省略号代替)

    地址: https://www.jianshu.com/p/229f96b794d3

  8. Nginx静态文件服务器配置方法

    在Java开发以及生产环境中,最常用的web应用服务器当属Tomcat,尽管这只猫也能够处理一些静态请求,例如图片.html.样式文件等,但是效率并不是那么尽人意.在生产环境中,我们一般使用Nginx ...

  9. Apollo配置中心环境搭建(Linux)

    官方教程:https://github.com/ctripcorp/apollo/wiki/Apollo-Quick-Start-Docker%E9%83%A8%E7%BD%B2 方式二:使用apol ...

  10. java 上传大文件以及文件夹

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...