Let's move on a little bit.
We now know how to make basic requests with urlLib.
You guys are going to become very friendly with that module.
I'd like to talk a little bit now is what we actually send over the wire between two computers.
We could have our servers--in this case we'll use the Hipmunk example.
We could have our servers make our request to Amtrak
and receive HTML back from them.
Then we can actually look into that HTML on Hipmunk servers.
That's actually what we do, but this is suboptimal.
Let me show you why. You've written some HTML at this point.
You know that it's somewhat complex.
It's not very regular.
You've got things like--browsers are very forgiving.
You can write HTML to look something like this where you have an opening tag,
and you have an opening
and you can forget to put the closing tag, put your closing tag,
and the browser will actually probably render it appropriately. At least some browsers will.
If you were a computer trying to parse this, you're expecting a
All of a sudden you can get lost in this loop.
Depending on how complicated you want to make your parser,
maybe you can recover from this like browsers do or maybe not.
But HTML is not an ideal language for computer-to-computer communication.
It turns on Amtrak, we actually get their HTML, and I'm going to show you some
of the heartache we have to go through to actually parse this HTML.
Remember I gave you some regular expressions during homework 2
to verify your quiz answers--to verify a username and an email.
These are a bunch of regular expressions that we actually use on Hipmunk
to parse Amtrak's HTML.
As you can see, this is just a wall of text.
This is extremely error-prone,
and you can see like we're actually looking for div with class availability.
We're going to look for the span whose ID is service_span.
This is what a time looks like. This is really nutso.
This is not the ideal way of doing things.
In a perfect world, we wouldn't have to hit Amtrak's webpage.
We would instead use an API that speaks a language more appropriate for this task.
Suck a language, if language is the correct word, might be XML.
XML is what actually invented in the late '90s specifically for this purpose--
to have a regular way of expression data between computer systems.
I can't claim to be the biggest fan of XML, but it is fairly easy to parse.
In fact, you've seen a lot of it.
So this is what some XML might look like.
If you're thinking this looks an awful lot like HTML, you are correct.
We have our first line, which is basically the document type.
We have the same thing in HTML.
Remember we've been using HTML5, so our doc type looks something like this.
It's just the first sign that says what format the rest of the document is.
Now, the reason both HTML and XML have doc types and this tag structure
is because they actually share a common ancestor in SGML,
which was invented in the '80s.
Now, the main difference between XML and HTML is in XML every tag
has to have a closing tag.
We've got opening , closing .
The tag format is the same.
We've still got out less thans and our greater thans and our slashes to indicate a closing tag.
But we have no void tags in XML.
Remember in HTML we could have the tag for putting in a line break,
and we never had a closing tag.
That's because HTML doesn't require all tags to close.
We have this notion of a void tag. The line break was an example of one of those.
It's just a opening tag. XML has nothing like that .
Now, if you want a tag that has no content in XML,
you could do something like this.
You could include a closing slash before you're greater than symbol.
In fact, there is actually a doc type for HMTL called "XHTML,"
which basically says my HTML document is actually going to be valid XML.
Instead of doing void tags with no closing slash,
you include the closing slash before the greater than.
You'll see that a lot in XML.
The whole point of what I'm trying to say is that XML is very similar to HTML,
but it's more rigorous. It's similar because they share the same ancestor.
Now, I'm not going to spend a whole lot more time on the structure of XML,
because we spent so much time on HTML already.
Just keep in mind that it's similar to HTML, but a little bit more consistent.