In this lesson, you’ll learn about: how to extract precise data from web pages using selectors, how CSS and XPath differ, and how to apply them effectively with real browser tools1. What is Data Extraction (“SQL for the Web”)🔹 Core IdeaData extraction is about selecting exactly what you want from a web page—just like SQL queries select rows from a database.Using tools like Beautiful Soup, you can:
Target specific elements
Extract clean text
Automate structured data collection
👉 Key Insight The power is not in scraping everything— it’s in extracting only what matters2. Understanding HTML Structure🔹 The DOM Tree Concept
Web pages are structured like a tree
Elements have:
Parents
Children
Siblings
👉 Example: Title $10 3. CSS Selectors (Your First Tool)🔹 Basics
Tag → div
Class → .price
ID → #main
🔹 Combining Selectorssoup.select("div.product span.price") 👉 This means:
Find span.price
Inside div.product
🔹 Why CSS is Powerful
Simple and readable
Fast to write
Works directly in browsers
4. XPath (Advanced Targeting)🔹 What is XPath?Use XPath
Treats HTML as a navigable tree
More flexible than CSS
🔹 Key Syntax
//div → find anywhere
/div → direct child
[@class="price"] → filter by attribute
🔹 Example//div[@class="product"]//span[@class="price"] 🔹 When XPath Wins
Complex structures
Conditional logic
Traversing up/down the tree
5. CSS vs XPath (Quick Comparison)FeatureCSSXPathEase of useEasyMediumPowerModerateHighReadabilityHighLowerComplex queriesLimitedStrong👉 Rule of Thumb
Start with CSS
Switch to XPath when needed
6. Using Chrome Developer Tools🔹 Inspecting ElementsSteps:
Right-click → Inspect
View HTML structure
Test selectors live
🔹 Pro Techniques1. Visual Debugging
Temporarily change styles:
background: orange; 👉 Confirms your selector targets the correct elements2. Copy Selectors Automatically
Right-click element → Copy →
CSS Selector
XPath
3. Test in Consoledocument.querySelectorAll("div.product") 7. Real-World Extraction Scenarios🔹 Example: Wikipedia Tables
Rendered via JavaScriptStored inside SVG elements👉 Solution:Inspect deeplyCheck network requestsReverse-engineer data source8. Best Practices for Clean Extraction🔹 Stay OrganizedWork step-by-stepTest selectors incrementally🔹 Write Robust SelectorsAvoid:div > div > div > span Prefer:.product .price 🔹 Expect ChangeWebsites update frequentlyBuild flexible logic9. Mental ModelHTML → Selector → Extract → Clean → Structure👉 Final Takeaway Mastering data extraction is less about tools and more about thinking structurally—once you understand how the web is built, you can query it with precision just like a database.