#100DaysOfCode in Python Transcripts
Chapter: Days 52-54: Parsing RSS feeds with Feedparser
Lecture: Feedparser Sanity Check
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
0:02
which is a bit of a best practice
0:04
as always with Python scripts, you would
0:06
normally put in some sort of an error check,
0:08
just to make sure or a conditional check,
0:10
just to make sure everything is in place
0:13
before you run your script, right.
0:16
So in this case, what happens if one
0:19
of these tags doesn't exist in the feed?
0:23
Well, sometimes these RSS feeds out there
0:28
don't always include the default tags,
0:32
like title and link or description
0:34
or whatever else, okay, if that happens
0:37
well then your script's going to break.
0:38
So you should try and capture those errors
0:41
but right now, I'm not going to walk you
0:42
through error catching and testing
0:45
and trying except and everything that's
0:47
another module in itself, right.
0:50
So what I will show is just a really
0:53
quick, if statement that just works, okay.
0:57
So you could do if and then your tag name,
1:01
now I would definitely insist on title being in there,
1:05
so if title in feed.entries, we're looking at the
1:11
first item in feed.entries there.
1:16
Then we want you to run the full loop and that's it,
1:21
okay, that's all I'm looking at now then you can put
1:26
ls break or something like that or you could run your
1:30
try and except and what if you want to wrap around it.
1:32
But in this case, that's all we need, so we can save that
1:37
and then we can run that, so Python
1:40
and we'll get the same output as last time.
1:45
Okay, we can go and see all of that data
1:51
now just to prove that this actually worked,
1:53
here's what we can do, let's clear and let's
2:01
change the actual tag we're looking for.
2:04
So let's come up with something that's
2:05
definitely not going to be in there.
2:09
Let's see if I can spell, so that should be in every feed
2:12
right but unfortunately not so if Julian rocks
2:16
in feed.entries then run your forward.
2:20
Okay, let's run that and bang, nothing
2:25
happened, let's put title back in and there we have it,
2:32
okay, so that's it if you want to do some
2:35
sort of testing against it before you run
2:38
it, well that's the way to do it.
2:40
Okay, and that's pretty much feed parser nice and simple.