Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 10: Navigating and Extracting Web Data with Beautiful Soup
Transcript
- 0:00Speaker 1: Depending on who you ask, up to ninety percent of
- 0:03Speaker 1: the Internet is actually built on broken code.
- 0:05Speaker 2: Oh easily ninety percent, yeah, right.
- 0:07Speaker 1: Like it is just full of missing brackets, unclosed tags,
- 0:10Speaker 1: and well formatting errors. If you try to read the
- 0:13Speaker 1: raw code of most websites with a basic Python script,
- 0:16Speaker 1: your code will break in seconds.
- 0:18Speaker 2: It's an absolute mess, it is.
- 0:20Speaker 1: Yet when you look at those same websites in your browser,
- 0:23Speaker 1: everything looks perfectly organized and beautiful. So you know, how
- 0:27Speaker 1: does your browser make sense of that chaos? And more importantly,
- 0:31Speaker 1: how can.
- 0:31Speaker 2: You that gap right there between the beautiful, user friendly
- 0:34Speaker 2: web page and the absolute jungle of raw code behind
- 0:38Speaker 2: the scenes. That's like one of the biggest hurdles when
- 0:41Speaker 2: you first start working with data. Yeah, for sure, you
- 0:43Speaker 2: might want to grab, say a table of stock prices
- 0:46Speaker 2: or a list of sports statistics, but opening up the
- 0:48Speaker 2: source code feels like trying to read a completely alien language.
- 0:53Speaker 1: Today's deep dive is designed to fix exactly that. We
- 0:56Speaker 1: are going to distill us down into a step by
- 0:58Speaker 1: step audio guide.
- 0:59Speaker 2: On webs no screens required.
- 1:01Speaker 1: Right, no streams. We're going to break down how web
- 1:04Speaker 1: pages are fundamentally structured and introduce a really powerful Python
- 1:10Speaker 1: library called beautiful Soup.
- 1:12Speaker 2: Such a great name it is.
- 1:13Speaker 1: And it basically magically turns that messy web data into
- 1:17Speaker 1: a clean, navigable format.
- 1:19Speaker 2: And like we said, we're doing this entirely as an
- 1:21Speaker 2: audio experience, so you don't need a text editor open,
- 1:24Speaker 2: and you definitely don't need to be staring at a
- 1:26Speaker 2: screen right now, just listen along exactly. We will explain
- 1:29Speaker 2: the logic and the workflow step by step, ending with
- 1:32Speaker 2: a quick mental review to solidify how this all works
- 1:35Speaker 2: in practice.
- 1:36Speaker 1: So to get to the data, we really have to
- 1:38Speaker 1: start at the foundation of how the data is stored.
- 1:41Speaker 1: When we write, click a web page and hit view source,
- 1:43Speaker 1: we get that intimidating wall of text. What is actually
- 1:46Speaker 1: going on in that document?
- 1:48Speaker 2: So you are looking at HTML, which stands for Hypertext
- 1:52Speaker 2: markup Language. It's the structural foundation of the way. But
- 1:56Speaker 2: the most critical concept of grasp here is that HTML's
- 2:00Speaker 2: not just a flat text document. You know, you don't
- 2:02Speaker 2: read it from top to bottom like a standard book.
- 2:03Speaker 1: Right, It's not a novel.
- 2:04Speaker 2: No, it's a strict hierarchical structure. In computer science, we
- 2:08Speaker 2: call this a parse tree. Your web browser reads this
- 2:12Speaker 2: tree to understand exactly where to place every image, paragraph,
- 2:16Speaker 2: and title on your screen.
- 2:18Speaker 1: So if it's a tree we are dealing with like
- 2:20Speaker 1: branches and leaves, yes.
- 2:22Speaker 2: Exactly, and every single element on that tree is called
- 2:25Speaker 2: a node. A node, right, A node is defined by tags.
- 2:29Speaker 2: You have an opening tag, the content inside, and a
- 2:32Speaker 2: closing tag. This will look like Well, the closing tag
- 2:35Speaker 2: looks identical to the opening one, except it includes a
- 2:38Speaker 2: forward slash. So if you want to create a paragraph,
- 2:41Speaker 2: you open it with a P inside angle brackets, type
- 2:44Speaker 2: your text and close it with a forward slash P
- 2:47Speaker 2: inside angled brackets.
- 2:49Speaker 1: So if I'm looking at the raw code and I
- 2:50Speaker 1: see those angled brackets with a slash, that immediately tells
- 2:53Speaker 1: me a specific branch of the tree has just ended.
- 2:56Speaker 2: That's right, and the entire tree stems from a single
- 2:58Speaker 2: starting point. At the very root of the document is
- 3:00Speaker 2: the HTML.
- 3:01Speaker 1: Node, the trunk of the tree.
- 3:02Speaker 2: Basically, yep, imagine an opening tag that just says htmo.
- 3:07Speaker 2: This root node contains everything else on the page as
- 3:10Speaker 2: its children. Specifically, the HTML root node has two immediate children,
- 3:15Speaker 2: the head node and the body note.
- 3:18Speaker 1: Okay, so if the root HTML tag splits into two
- 3:21Speaker 1: main branches, the head and the body. I'm guessing the
- 3:23Speaker 1: body is what I actually see on the page, and
- 3:25Speaker 1: the head is like the behind the scenes stuff.
- 3:27Speaker 2: That division of labor is exactly how it works. You
- 3:31Speaker 2: really did. The head node contains metadata. It holds information
- 3:36Speaker 2: about the web page that isn't visible in the main window,
- 3:39Speaker 2: like what like the character encoding or the title of
- 3:41Speaker 2: the web page that shows up way at the top
- 3:43Speaker 2: in your browser tab. Then the second child, the body node,
- 3:48Speaker 2: contains the actual viewport content, all the texts, the images,
- 3:51Speaker 2: the links, the tables that all lives nested inside the body.
- 3:55Speaker 1: Note and because the head and the body are both
- 3:57Speaker 1: immediate children stemming directly from the root html, well know
- 4:00Speaker 1: they are sitting right next to each other in the hierarchy.
- 4:02Speaker 2: They are siblings, siblings right, The body is the next
- 4:05Speaker 2: sibling after the head, and inside that body sibling, the
- 4:08Speaker 2: branching continues. You have more children, just turtles all the
- 4:11Speaker 2: way down. Basically, Yeah, you might have header tags like
- 4:15Speaker 2: an H one or an H two, which create large
- 4:18Speaker 2: texts for titles. You might have paragraph tags. You'll definitely
- 4:21Speaker 2: have anchor tags represented by the letter A, which create
- 4:24Speaker 2: clickable links pointing to other URLs.
- 4:27Speaker 1: Okay, let me try to map this out visually.
- 4:29Speaker 2: Go for it.
- 4:29Speaker 1: The structure is basically a massive family tree or like
- 4:33Speaker 1: Russian nesting dolls, good analogy. The entire document itself is
- 4:37Speaker 1: the great grandparent. HTML tag is the grandparent. The head
- 4:41Speaker 1: and body tags are the parents who are siblings to
- 4:43Speaker 1: each other, and then the text, the links, the images,
- 4:46Speaker 1: and the headers inside of the body those are the
- 4:49Speaker 1: children and grandchildren.
- 4:51Speaker 2: That mental model is exactly how you need to approach
- 4:53Speaker 2: webscraping when you want to extract specific data. You aren't
- 4:57Speaker 2: just doing a word search. You are navigating a family
- 5:00Speaker 2: tree to locate a very specific child.
- 5:03Speaker 1: Node, which brings us to the tool we actually use
- 5:06Speaker 1: to do that navigating. How do we talk to this
- 5:08Speaker 1: family tree using Python?
- 5:10Speaker 2: We use a Python library called beautiful Soup.
- 5:12Speaker 1: Still love that name.
- 5:13Speaker 2: It's currently on version four, and you can add it
- 5:16Speaker 2: to your environment by just running a PIP install command in.
- 5:20Speaker 1: Your terminal For anyone who might be newer to Python environments.
- 5:24Speaker 1: PIP is basically the built in app store for Python. Right,
- 5:28Speaker 1: you just tell PIP to install beautiful Soup and it
- 5:30Speaker 1: downloads the tools you need.
- 5:32Speaker 2: Yeah, it's Python's package manager, It goes out, grabs the
- 5:35Speaker 2: beautiful soup code makes it available for your scripts. The
- 5:38Speaker 2: main job of this library is to ingest that massive
- 5:42Speaker 2: raw HTML string, that wall of text, and automatically convert
- 5:46Speaker 2: it into a navigable parse tree object, which we call
- 5:49Speaker 2: We literally just call it soup in the code perfect.
- 5:51Speaker 2: Once you have that soup object, you can navigate the
- 5:54Speaker 2: family tree using simple dot notation.
- 5:56Speaker 1: Wait, so I just use a period to traverse down
- 5:59Speaker 1: the tree.
- 5:59Speaker 2: Yes, if you want to grab the title of the page,
- 6:02Speaker 2: you type soup dot title. If you want the body,
- 6:04Speaker 2: you type soup dot body.
- 6:06Speaker 1: Wow, that's easy.
- 6:07Speaker 2: It understands the hierarchy. So every element also knows who
- 6:10Speaker 2: his parent is. If you isolate the body tag and
- 6:13Speaker 2: then ask for its parent by typing soup dot parent
- 6:16Speaker 2: dot name, it will literally spit back HTML.
- 6:19Speaker 1: Okay, but if I ask for soup dot title. There's
- 6:22Speaker 1: generally only one title on a page. But if I
- 6:24Speaker 1: type soup dot P to get a paragraph, or soup
- 6:26Speaker 1: dot A to get a link, a typical web page
- 6:29Speaker 1: has dozens or hundreds of those. What does the soup
- 6:32Speaker 1: object actually hand back to me?
- 6:34Speaker 2: It will only return the very first instance it finds
- 6:37Speaker 2: only the first one. Yep, soup dot P gives you
- 6:40Speaker 2: the first paragraph in the document, soup dot A gives
- 6:43Speaker 2: you the first link. It completely stops searching the moment
- 6:46Speaker 2: it bumps into a child that matches your request.
- 6:49Speaker 1: Okay, I want to push back on the whole concept
- 6:51Speaker 1: of needing this library for a second.
- 6:53Speaker 2: Okay, let's hear it.
- 6:53Speaker 1: If HTML is ultimately just text data, just characters on
- 6:57Speaker 1: a screen, and we only want the first paragraph or
- 6:59Speaker 1: the first link, wait, I mean, why rely on a
- 7:02Speaker 1: special tool at all? Can't we just use standard Python
- 7:05Speaker 1: text search or regular expressions to say, you know, find
- 7:09Speaker 1: the bracket, find the letter P, and grab the words
- 7:11Speaker 1: next to it.
- 7:12Speaker 2: I mean, in theory, yes, if every web developer wrote
- 7:16Speaker 2: perfect code, regular expressions would work.
- 7:18Speaker 1: But they don't, not even close.
- 7:21Speaker 2: Real world HTML is spectacularly messy. People forget to close
- 7:25Speaker 2: their H one header tags, They nest tags improperly, they
- 7:28Speaker 2: leave elements dangling without closing bracket.
- 7:30Speaker 1: It's the wild West exactly.
- 7:32Speaker 2: If you try to use basic text search on ill
- 7:35Speaker 2: formatted HTML, your code will just fail to find what
- 7:38Speaker 2: you want, or worse, it will extract half the page
- 7:41Speaker 2: by accident.
- 7:41Speaker 1: So the family tree is broken. You have parents without
- 7:44Speaker 1: children and branches that just end in thin air.
- 7:47Speaker 2: And that is the true power of Beautiful Soup. It
- 7:50Speaker 2: doesn't just read the tree, it manages the chaos. Yeah,
- 7:53Speaker 2: but Beautiful Soup can't actually do that alone. It requires
- 7:58Speaker 2: a parser to act as the engine that actually reads
- 8:00Speaker 2: the raw text and builds the tree in the background.
- 8:03Speaker 2: When you create your soup object in Python, you have
- 8:05Speaker 2: to specify which partser you want to use.
- 8:08Speaker 1: So the parser is the thing actually interpreting the broken
- 8:11Speaker 1: HTML and trying to make sense of it. What are
- 8:13Speaker 1: our options?
- 8:13Speaker 2: There are three main parsers you need to understand. The
- 8:15Speaker 2: first is called XML XML got it, Yeah, And if
- 8:19Speaker 2: you don't specify one, Beautiful Soup will often default to
- 8:23Speaker 2: XML depending on your machine's setup. It is incredibly fast
- 8:26Speaker 2: and highly performant.
- 8:27Speaker 1: Why is it so much faster than the others.
- 8:29Speaker 2: Because XML relies on an external C library. C is
- 8:34Speaker 2: the lower level programming language than Python, meaning it talks
- 8:37Speaker 2: much closer to the machine's hardware. Right, so when you
- 8:40Speaker 2: pass the heavy lifting of reading text down the sea,
- 8:43Speaker 2: it processes massive documents in a fraction of the time.
- 8:46Speaker 1: That makes sense.
- 8:46Speaker 2: Aside from speed, XML is also lenient. If a developer
- 8:50Speaker 2: forgot to put a closing tag on a paragraph, XML
- 8:53Speaker 2: figures out what was intended and automatically adds the closing
- 8:57Speaker 2: tag into the parse tree for you.
- 8:58Speaker 1: Okay, so fast, hardware, liable processing, and it fixes typos
- 9:02Speaker 1: on the fly. What is the second option?
- 9:03Speaker 2: The second is HTML dot parser. This comes built in
- 9:06Speaker 2: with Python, so you don't need to install any external
- 9:08Speaker 2: CE dependencies.
- 9:09Speaker 1: Oh die.
- 9:10Speaker 2: It operates at a decent speed and is fairly lenient,
- 9:12Speaker 2: kind of sitting right in the middle ground. It's not
- 9:15Speaker 2: as fast as XML, and it's not quite as forgiving
- 9:18Speaker 2: as the third option, which is what HTML five limb.
- 9:21Speaker 2: This parser is designed to read messy web pages the
- 9:24Speaker 2: exact same way a modern web browser like Chrome or
- 9:27Speaker 2: Safari wood. If you feed it absolute garbage HTML, it
- 9:31Speaker 2: will meticulously figure it out and generate perfectly valid, strictly
- 9:35Speaker 2: formatted HTML five code.
- 9:37Speaker 1: Wait, if it builds perfectly valid HTML, why wouldn't we
- 9:40Speaker 1: just use HTML five lib every single time?
- 9:42Speaker 2: Because that perfection requires a massive amount of computational power.
- 9:47Speaker 2: HTML five lib is effectively running a simulation of a
- 9:50Speaker 2: full browser engine just to parse text. Oh wow. As
- 9:54Speaker 2: a result, it is incredibly slow compared to XML, and
- 9:57Speaker 2: it requires you to install another extern Python library dependency.
- 10:01Speaker 1: Okay, let me synthesize this. Parsers are essentially different types
- 10:05Speaker 1: of proofreaders you hire to read a messy manuscript I
- 10:07Speaker 1: like this. LXML is the speed reader. They fly through
- 10:09Speaker 1: the text, fix the obvious typos without bothering you, and
- 10:12Speaker 1: get the job done fast. HTML dot parser is your
- 10:14Speaker 1: standard in house editor, reliable and built right into the company.
- 10:18Speaker 1: And HTML five lib is the agonizingly slow perfectionist who
- 10:21Speaker 1: rewrites every single sentence to make sure it adheres to
- 10:24Speaker 1: the modern style guide.
- 10:25Speaker 2: That analogy hits the core differences perfectly. For the vast
- 10:29Speaker 2: majority of webscraping tasks you are going to want. The
- 10:32Speaker 2: speed reader LSML gives you the performance you need while
- 10:35Speaker 2: fixing enough of the broken code to let you actually
- 10:38Speaker 2: extract your data.
- 10:39Speaker 1: So we have a blueprint of the tree, We have
- 10:41Speaker 1: beautiful soup, we have our proofreading parsers. How do we
- 10:44Speaker 1: actually put this into a workflow. We have a philosophical model,
- 10:47Speaker 1: but in the real world, how do we reach out
- 10:49Speaker 1: and grab a web page without crashing our script?
- 10:52Speaker 2: Well, the workflow starts before beautiful Soup even gets involved. First,
- 10:55Speaker 2: you need to retrieve the web page. In Python, you
- 10:59Speaker 2: typically use a library called requests to make a GID
- 11:02Speaker 2: request to a URL. Okay, then you check the response
- 11:05Speaker 2: to ensure you received a two hundred status code before
- 11:08Speaker 2: moving forward.
- 11:09Speaker 1: Let's break that jargon down really quick. A GID request
- 11:13Speaker 1: is exactly what it sounds like. The script is sending
- 11:15Speaker 1: a message to the website server saying, hey, get me
- 11:18Speaker 1: this file and the two hundred status code.
- 11:20Speaker 2: That's the universal HTT code for success. When you ask
- 11:24Speaker 2: a server for a file, it sends back a code.
- 11:26Speaker 2: You might be familiar with, a four or four code,
- 11:28Speaker 2: which means.
- 11:29Speaker 1: Not found, right, We've all seen that one.
- 11:31Speaker 2: A two hundred code means the server successfully found the
- 11:34Speaker 2: page and is handing it over. Once you verify that
- 11:37Speaker 2: two hundred success, you take the dot text property of
- 11:40Speaker 2: the server's response. That dot text is your raw MESSYHTML
- 11:44Speaker 2: string none, and then you feed that string, along with
- 11:47Speaker 2: your chosen parser, directly into beautiful Soup.
- 11:51Speaker 1: So Requests fetches the messy manuscript, and Beautiful Soup reads
- 11:55Speaker 1: it and builds the family tree exactly but trying to
- 11:58Speaker 1: scrape a live website right out of the gate. It
- 12:00Speaker 1: feels like a recipe for frustration if you're just learning.
- 12:02Speaker 1: Like live websites change constantly.
- 12:05Speaker 2: They do, and that is a major pitfall for beginners.
- 12:08Speaker 2: A web developer might decide to change an H one
- 12:10Speaker 2: tag to an H two tag on a Tuesday afternoon.
- 12:13Speaker 2: If you are scraping the live web, your script suddenly
- 12:16Speaker 2: breaks and you'll assume your code is wrong, when in reality,
- 12:19Speaker 2: the structure of the family tree just shifted.
- 12:21Speaker 1: So how do you practice without the ground moving underneath?
- 12:23Speaker 2: You create a simple local HTML file on your own computer.
- 12:27Speaker 1: Oh, like a dummy page.
- 12:29Speaker 2: Yeah, write a basic fan page for Giant pand is.
- 12:33Speaker 2: Put in a header, some paragraphs, a table of what
- 12:35Speaker 2: pandes eat, and maybe an image. Save it to your
- 12:37Speaker 2: hard drive. Is giant panded out eachtml Nice? You can
- 12:41Speaker 2: use Python's basic file reading commands to open that local
- 12:44Speaker 2: file and pass his contents directly into beautiful soup. Because
- 12:47Speaker 2: you control the file, the structure will never change unexpectedly.
- 12:50Speaker 1: Okay, so we feed our local giant Pano file into
- 12:53Speaker 1: beautiful soup. We have our soup object. When we put
- 12:56Speaker 1: it out, does it still look like a chaotic wall
- 12:58Speaker 1: of text?
- 13:00Speaker 2: If you use the dot Pritiphi method. Yeah, if you
- 13:02Speaker 2: call soup dot pritiphi, beautiful Soup prints the HTML contents
- 13:07Speaker 2: out with proper indentation. It visually nests all the children
- 13:11Speaker 2: under their parents, making the raw code incredibly easy for
- 13:15Speaker 2: a human to read and understand.
- 13:16Speaker 1: Now we want the actual data. If I call soup
- 13:19Speaker 1: dot A to get a link, I know it gives
- 13:21Speaker 1: me the HTML tag, But an anchor tag isn't the
- 13:23Speaker 1: actual web address, It's just the container. How do I
- 13:25Speaker 1: get the specific URL it's coining to?
- 13:27Speaker 2: So tags have attributes. An anchor tag the A tag
- 13:31Speaker 2: uses an ref attribute to store the destination URL. Beautiful
- 13:35Speaker 2: Soup allows you to access these attributes exactly like a
- 13:38Speaker 2: Python dictionary.
- 13:39Speaker 1: Okay, how does that look?
- 13:40Speaker 2: You ask for the tag and then request the attribute
- 13:43Speaker 2: in square brackets, so soup dot atrip will return just
- 13:46Speaker 2: the raw URL.
- 13:47Speaker 1: Strength Wait does that work for images too? If I
- 13:49Speaker 1: want the picture of the giant panda and I type
- 13:51Speaker 1: soup dot img, what happens?
- 13:53Speaker 2: It returns the HTML tag pointing to the image, not
- 13:56Speaker 2: the actual media file. Remember beautiful soup strictly partsis.
- 13:59Speaker 1: Teg right, it's just a text tree to.
- 14:01Speaker 2: Find where the image lives. You look at the tags
- 14:04Speaker 2: source attribute, which is written as src. Calling su dot
- 14:08Speaker 2: imgsrc hands you the URL of the image file so
- 14:11Speaker 2: you can go download it yourself later.
- 14:12Speaker 1: Okay, what if a single element has multiple things attached
- 14:16Speaker 1: to it, like styling classes. Web developers use CSS classes
- 14:19Speaker 1: to change colors and fonts, and an element might have
- 14:22Speaker 1: like three different classes applied to it at once.
- 14:25Speaker 2: Beautiful soup anticipates that if you ask for a tags
- 14:28Speaker 2: class attribute like soup dot piece class, it won't return
- 14:32Speaker 2: a single string. It recognizes that classes can stack, so
- 14:36Speaker 2: it returns a Python list containing all the classes applied
- 14:38Speaker 2: to that specific paragraph.
- 14:40Speaker 1: Okay, so we've covered tags and attributes, but what if
- 14:42Speaker 1: we have a table of data on our panda page,
- 14:45Speaker 1: say a list of different foods pandas eat and how
- 14:48Speaker 1: many pounds they consume. How do we get the actual
- 14:51Speaker 1: words out of a table?
- 14:53Speaker 2: This is where we need to move beyond just asking
- 14:55Speaker 2: for the first tag. A table in HTML is built
- 14:58Speaker 2: using row tags, which are written as TR for table row.
- 15:02Speaker 2: Inside each row you have data cells written as totr
- 15:05Speaker 2: for table data. Right, you just type soup dot tr
- 15:07Speaker 2: You only get the first row, which is usually the headers.
- 15:09Speaker 1: But we want every row in the table so we
- 15:11Speaker 1: can see the whole diet exactly.
- 15:13Speaker 2: To get all of them, you use a method called
- 15:15Speaker 2: dot find all. If you write soup dot findel trt,
- 15:19Speaker 2: Beautiful Soup scans the entire document and returns a list
- 15:22Speaker 2: containing every single table row note it found.
- 15:25Speaker 1: And because it returns a list, I can just use
- 15:27Speaker 1: a standard Python for loop. I can tell Python for
- 15:30Speaker 1: every row in this list of rows, look inside and
- 15:33Speaker 1: pull out the tatty data sell precisely.
- 15:35Speaker 2: You iterate through the rows, isolate the data cells, and
- 15:37Speaker 2: then to get the actual text, you call the dot string.
- 15:40Speaker 1: Property that string property.
- 15:42Speaker 2: Yes, if you append dot string to a tag, Beautiful
- 15:45Speaker 2: Soup strips away all the brackets and markup language and
- 15:48Speaker 2: just hands you the raw text hidden inside. It returns
- 15:51Speaker 2: a navigable string object. It can even extract hidden HTML
- 15:55Speaker 2: comments left behind by the developer.
- 15:56Speaker 1: We loop through the rows, pull out the text, and
- 15:58Speaker 1: suddenly we have cleaned the Beautiful Soup isn't strictly read only,
- 16:03Speaker 1: is it?
- 16:03Speaker 2: No? Not at all. It is fully dynamic. You can
- 16:06Speaker 2: programmatically access a tag and change its dot name property
- 16:09Speaker 2: to a completely different custom tag. You can rewrite the
- 16:12Speaker 2: text inside. Wow, you can essentially rewrite the structural DNA
- 16:15Speaker 2: of the web page in your system's memory and then
- 16:18Speaker 2: save that modified soup object out as a brand new
- 16:21Speaker 2: HTML file.
- 16:22Speaker 1: That is incredibly powerful. Let's summarize the journey we just took.
- 16:27Speaker 1: We started with the concept that web pages are not
- 16:29Speaker 1: flat text. They are hierarchical parse trees built on HTML
- 16:33Speaker 1: filled with parent, child and sibling nodes.
- 16:36Speaker 2: Correct.
- 16:36Speaker 1: We introduced beautiful soup as a tool to navigate that tree,
- 16:39Speaker 1: and we discussed how parsers like the high Speed XML
- 16:42Speaker 1: or the meticulous HTML five lab act as the engines
- 16:45Speaker 1: to interpret broken code. From there, we covered making a
- 16:49Speaker 1: JDT request, checking for a two hundred success status, and
- 16:53Speaker 1: using dot notation and find all to extract attributes, lists
- 16:57Speaker 1: and raw text.
- 16:58Speaker 2: To reinforce how that's in works in practice. Let's do
- 17:01Speaker 2: a quick mental exercise for everyone listening.
- 17:03Speaker 1: Let's do it.
- 17:03Speaker 2: Imagine you are scraping a web page that has ten
- 17:06Speaker 2: different bolded words scattered throughout the paragraphs. The HTML tag
- 17:10Speaker 2: for bold text is simply the letter B. If you
- 17:13Speaker 2: tube soup dot b in your Python script, Well, what
- 17:17Speaker 2: exactly will beautiful soup hand back to you.
- 17:19Speaker 1: I know this one. It will hand back just the
- 17:21Speaker 1: very first bolded word it encounters in the document and
- 17:23Speaker 1: completely ignore the other nine Bingo.
- 17:26Speaker 2: It always returns the first instance it finds in the hierarchy,
- 17:30Speaker 2: unless you specifically ask for the full list using dot findal.
- 17:35Speaker 1: To wrap this up, let's look at the broader implications
- 17:37Speaker 1: of this. We talked about how the dot string property
- 17:40Speaker 1: can extract hidden HTML comments that developers leave in the code,
- 17:44Speaker 1: things that are completely invisible when you just look at
- 17:47Speaker 1: the page in a browser.
- 17:48Speaker 2: Yeah, it pulls back the curtain on everything the server
- 17:50Speaker 2: sends to your machine, whether the browser decides to render
- 17:53Speaker 2: it on the screen or not.
- 17:54Speaker 1: So the provocative thought for you to take away is this,
- 17:57Speaker 1: if beautiful soup can so easily scrape hit in text,
- 18:00Speaker 1: metadata and structural tags, what invisible data is currently sitting
- 18:06Speaker 1: right beneath the surface of your favorite social media site,
- 18:08Speaker 1: just waiting in the raw HTML for you to extract?
- 18:11Speaker 1: It makes you wonder it really does. That chaotic wall
- 18:13Speaker 1: of text isn't a wall at all. It's just a
- 18:16Speaker 1: perfectly organized set of nesting dolls, and you now have
- 18:19Speaker 1: the tools to open them. Thanks for joining us on
- 18:21Speaker 1: this deep dive. We'll catch you next time.