Skip to content
Artwork for CyberCode Academy
CyberCode Academy ยท July 27 ยท 21 min

Course 40 - Web Scraping with Python | Episode 17: Mastering Requests, Regex, and Beautiful Soup

In this lesson, youโ€™ll learn about: how Python retrieves web pages, how regex is used for pattern-based extraction, and how BeautifulSoup improves scraping by understanding HTML structure instead of treating it as plain text1. Fetching Web Content in Python๐Ÿ”น HTTP Request FlowWeb scraping always starts with getting the page content.๐Ÿ”น Libraries Used urllib โ†’ built-in, basic control httplib2 โ†’ low-level control requests โ†’ easiest and most popular ๐Ÿ”น Requests Exampleimport requests response = requests.get("https://example.com") html = response.text ๐Ÿ”น User-Agent HandlingSome sites block bots, so you can:headers = {"User-Agent": "Mozilla/5.0"} requests.get(url, headers=headers) ๐Ÿ‘‰ Key Insight Without proper headers, many sites will reject your scraper2. Regular Expressions (Regex Basics)๐Ÿ”น Pattern Matching ConceptRegex treats web data as raw text patterns.3. Core Regex FunctionsFunctionBehaviormatch()checks start onlysearch()finds first match anywherefindall()returns all matches๐Ÿ”น Special SymbolsSymbolMeaning\ddigits\wletters + numbers\swhitespace๐Ÿ”น Example Patternimport re re.findall(r"\d+", "Price is 123 dollars") ๐Ÿ‘‰ Key Insight Regex is powerful but fragile for HTML4. Advanced Regex Techniques๐Ÿ”น Ranges & Groups [A-Z] โ†’ uppercase letters {3} โ†’ exact repetition ( ) โ†’ capture groups ๐Ÿ”น Example: Extract Namesre.search(r"(\w+) (\w+)", "John Smith") 5. Real Web Scraping Use Cases๐Ÿ”น Inspecting HTMLUsing browser tools, you can locate: items , headerscontact detailslocation data๐Ÿ”น Example Targets Phone numbers Zip codes City/state data 6. BeautifulSoup (Structured Parsing)๐Ÿ”น DOM-Based ApproachBeautifulSoup understands HTML as a tree structure, not text.๐Ÿ”น Basic Usagefrom bs4 import BeautifulSoup soup = BeautifulSoup(html, "lxml") print(soup.title.string) ๐Ÿ”น Key Advantage Navigates tags easily Handles broken HTML Cleaner extraction than regex 7. Parsers (LXML vs HTML5lib)ParserStrengthlxmlfasthtml5libvery forgiving๐Ÿ‘‰ Key Insight Parser choice affects speed vs accuracy8. Regex vs BeautifulSoupFeatureRegexBeautifulSoupStructure awareโŒโœ”๏ธSpeedโœ”๏ธMediumReliabilityโŒโœ”๏ธ9. Mental ModelThink of scraping like: ๐Ÿ“ฅ Requests โ†’ download page ๐Ÿ” Regex โ†’ pattern hunting ๐ŸŒณ BeautifulSoup โ†’ structured navigation Final TakeawayWeb scraping becomes powerful when you stop treating HTML as text and start treating it as a structured tree of data.๐Ÿ‘‰ Use: Requests โ†’ fetch Regex โ†’ quick patterns BeautifulSoup โ†’ real extraction That combination covers most real-world scraping tasks. You can listen and download our episodes for free on more than 10 different platforms: https://linktr.ee/cybercode_academy

0:00-21:19

transcript

No transcript โ€” this publisher did not publish one.

show notes

In this lesson, youโ€™ll learn about: how Python retrieves web pages, how regex is used for pattern-based extraction, and how BeautifulSoup improves scraping by understanding HTML structure instead of treating it as plain text1. Fetching Web Content in Python๐Ÿ”น HTTP Request FlowWeb scraping always starts with getting the page content.๐Ÿ”น Libraries Used
  • urllib โ†’ built-in, basic control
  • httplib2 โ†’ low-level control
  • requests โ†’ easiest and most popular
๐Ÿ”น Requests Exampleimport requests response = requests.get("https://example.com") html = response.text ๐Ÿ”น User-Agent HandlingSome sites block bots, so you can:headers = {"User-Agent": "Mozilla/5.0"} requests.get(url, headers=headers) ๐Ÿ‘‰ Key Insight
Without proper headers, many sites will reject your scraper2. Regular Expressions (Regex Basics)๐Ÿ”น Pattern Matching ConceptRegex treats web data as raw text patterns.3. Core Regex FunctionsFunctionBehaviormatch()checks start onlysearch()finds first match anywherefindall()returns all matches๐Ÿ”น Special SymbolsSymbolMeaning\ddigits\wletters + numbers\swhitespace๐Ÿ”น Example Patternimport re re.findall(r"\d+", "Price is 123 dollars") ๐Ÿ‘‰ Key Insight
Regex is powerful but fragile for HTML4. Advanced Regex Techniques๐Ÿ”น Ranges & Groups
  • [A-Z] โ†’ uppercase letters
  • {3} โ†’ exact repetition
  • ( ) โ†’ capture groups
๐Ÿ”น Example: Extract Namesre.search(r"(\w+) (\w+)", "John Smith") 5. Real Web Scraping Use Cases๐Ÿ”น Inspecting HTMLUsing browser tools, you can locate:
  • items
, headerscontact detailslocation data๐Ÿ”น Example Targets
  • Phone numbers
  • Zip codes
  • City/state data
6. BeautifulSoup (Structured Parsing)๐Ÿ”น DOM-Based ApproachBeautifulSoup understands HTML as a tree structure, not text.๐Ÿ”น Basic Usagefrom bs4 import BeautifulSoup soup = BeautifulSoup(html, "lxml") print(soup.title.string) ๐Ÿ”น Key Advantage
  • Navigates tags easily
  • Handles broken HTML
  • Cleaner extraction than regex
7. Parsers (LXML vs HTML5lib)ParserStrengthlxmlfasthtml5libvery forgiving๐Ÿ‘‰ Key Insight
Parser choice affects speed vs accuracy8. Regex vs BeautifulSoupFeatureRegexBeautifulSoupStructure awareโŒโœ”๏ธSpeedโœ”๏ธMediumReliabilityโŒโœ”๏ธ9. Mental ModelThink of scraping like:
  • ๐Ÿ“ฅ Requests โ†’ download page
  • ๐Ÿ” Regex โ†’ pattern hunting
  • ๐ŸŒณ BeautifulSoup โ†’ structured navigation
Final TakeawayWeb scraping becomes powerful when you stop treating HTML as text and start treating it as a structured tree of data.๐Ÿ‘‰ Use:
  • Requests โ†’ fetch
  • Regex โ†’ quick patterns
  • BeautifulSoup โ†’ real extraction
That combination covers most real-world scraping tasks.

You can listen and download our episodes for free on more than 10 different platforms:
https://linktr.ee/cybercode_academy
links1