#100DaysOfCode in Python Transcripts
Chapter: Days 52-54: Parsing RSS feeds with Feedparser
Lecture: Parsing XML with Feedparser
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Okay, one last little thing for you which is a bit of a best practice as always with Python scripts, you would
0:07
normally put in some sort of an error check, just to make sure or a conditional check, just to make sure everything is in place
0:14
before you run your script, right. So in this case, what happens if one of these tags doesn't exist in the feed?
0:24
Well, sometimes these RSS feeds out there don't always include the default tags, like title and link or description
0:35
or whatever else, okay, if that happens well then your script's going to break. So you should try and capture those errors
0:42
but right now, I'm not going to walk you through error catching and testing and trying except and everything that's another module in itself, right.
0:51
So what I will show is just a really quick, if statement that just works, okay. So you could do if and then your tag name,
1:02
now I would definitely insist on title being in there, so if title in feed.entries, we're looking at the first item in feed.entries there.
1:17
Then we want you to run the full loop and that's it, okay, that's all I'm looking at now then you can put
1:27
ls break or something like that or you could run your try and except and what if you want to wrap around it.
1:33
But in this case, that's all we need, so we can save that and then we can run that, so Python and we'll get the same output as last time.
1:46
Okay, we can go and see all of that data now just to prove that this actually worked, here's what we can do, let's clear and let's
2:02
change the actual tag we're looking for. So let's come up with something that's definitely not going to be in there.
2:10
Let's see if I can spell, so that should be in every feed right but unfortunately not so if Julian rocks in feed.entries then run your forward.
2:21
Okay, let's run that and bang, nothing happened, let's put title back in and there we have it, okay, so that's it if you want to do some
2:36
sort of testing against it before you run it, well that's the way to do it. Okay, and that's pretty much feed parser nice and simple.