Worked Example- XML (Chapter 13)

دوره: Using Python to Access Web Data / فصل: Web Services and XML (Chapter 13) / درس 5

Worked Example- XML (Chapter 13)

توضیح مختصر

And so later we'll be pulling XML and JSON from the web, but for now I'll just going to put it in a triple coded string, so data. Now we have to call this ET from string to read this and give us back a tree object. And so, that's just a basic run through of the XML from the chapter in the Python book, okay?

  • زمان مطالعه 6 دقیقه
  • سطح خیلی ساده

دانلود اپلیکیشن «زوم»

این درس را می‌توانید به بهترین شکل و با امکانات عالی در اپلیکیشن «زوم» بخوانید

دانلود اپلیکیشن «زوم»

فایل ویدیویی

متن انگلیسی درس

Hello, and welcome to Python for Everybody. I’m Charles Severance. I’m the author of the textbook and we’re going to do a little bit of code. If you want to get your hands on the code, go to the materials website, marterials.php. Actually materials.php and download the sample code. The code that we’re going to work on today is the XML code. And we need to be able to talk XML to work with web services. And so here’s one of the examples from the book. It’s XML1.py. And so later we’ll be pulling XML and JSON from the web, but for now I’ll just going to put it in a triple coded string, so data. And we’re going to use our built-in XML parser in Python called ElementTree. And when we say, import xml.etree.ElementTree as ET. This as ET gives us basically a shortcut handle for it. And so the idea, this is a string that has less thans and greater thans. It looks like structured information, and it is, but really at this point, it’s only a string. Now we have to call this ET from string to read this and give us back a tree object. Now what it does is this might blow up. This code might blow up right here if there was a mistake in it, okay? And matter of fact, I can probably put a mistake in. Let’s see if I can delete this and save it and run this code and we’ll see that it will blow up. Right? And so it blew up here in Line A, ElementTree blew up. It blew up in line 12 of the code, which is right here. This failed because the line 8 of the XML string was wrong. So let’s put that back in, so now it’s properly formed. From XML. So, this tree we get back, I name it tree just because I always name it tree, but you could name it X. So, the key is is tree.find goes and looks for a tag named find and tree has no longer got less thans and greater thans in it. It has went and turned these into objects within objects within objects. So, tree find name says that I would like to find the tag name, and that’s what this bit is right here. And then .txt is going within that and grabbing that text, okay? And if we say tree.find(‘email’), then that’s going to give us this. And then that’s that object. And then .get asks for the contents of the hide attribute, which is the string yes. Okay and so if we run this now that it’s fixed Python3 xml3.py. It will pull in and get the app the name and the attributes. So it pulled the . So you get this object and then you kind of dive into that object. So that’s xml1.py, if you got a tag you can either get the text out of the tag or you can get an attribute out of the tag. So now let’s take a look at xml2.py. Again, we import ElementTree and we have a tag. And XML’s always gotta have a single outer tag. But this time, we’re going to have, in effect, a list. And let’s line this up a little better. There we go, that looks a little prettier. And so, users, the fact that it’s users doesn’t mean anything, but we often come up with syntactically meaningful names for these things. Users is going to have as a children, a list of user tags. Okay so the children under user. User under user and then this has each of these as a tag. So we want to parse this and this is a common thing we want to do. So, again, the first thing we do is we read the string to just take this. It’s a triple coded string going from here to here. And then we’re going to instead of doing find, which gives us one tag, we’re going to do find all, with the users tag, the user tag that is a child of users. And we get back a python list of the tags, not of the text but of the tags. So there is a one tag and there is another tag. And you do learn of that so you can see that we got two and then we can write a for-loop. And this item is going to iterate through the tags that are the user tags they are children of users. So the first time item is going to be this tag, a tag remember, and the second time is going to be this tag. And so we can do things like find and get, just like we did in xml1. So running this is not too exciting. Python3 xml2.py. You see that there are two users that comes from this print right here. There are two users in there, and the first one, if we go into name, and we go find a text within the name tag within user then we get Chuck and then we get the id which is 001. So we find the id within that item and then we get the text. And then we look and we grab the X attribute off of that. And so we see Chuck. Chuck 001 and 2 and then the next tag we get a for loop continues. And we print that out, okay? And so, that’s just a basic run through of the XML from the chapter in the Python book, okay? Thanks.

مشارکت کنندگان در این صفحه

تا کنون فردی در بازسازی این صفحه مشارکت نداشته است.

🖊 شما نیز می‌توانید برای مشارکت در ترجمه‌ی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.