Bird
Raised Fist0
LangChainframework~10 mins

Loading web pages with WebBaseLoader in LangChain - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Loading web pages with WebBaseLoader
Start
↓
Create WebBaseLoader with URL
↓
Call load() method
↓
Fetch web page content
↓
Parse and store content
↓
Return loaded documents
↓
End
This flow shows how WebBaseLoader takes a URL, fetches the web page content, parses it, and returns it as documents.
Execution Sample
LangChain
from langchain.document_loaders import WebBaseLoader
loader = WebBaseLoader('https://example.com')
docs = loader.load()
print(docs[0].page_content[:100])
This code loads the content of 'https://example.com' using WebBaseLoader and prints the first 100 characters.
Execution Table
StepActionInput/StateOutput/State
1Create WebBaseLoaderURL='https://example.com'loader instance created with URL
2Call load()loader instanceStarts fetching web page content
3Fetch contentHTTP GET request to URLRaw HTML content received
4Parse contentRaw HTML contentParsed text extracted from HTML
5Store contentParsed textDocument object created with page_content
6Return documentsDocument objectList of documents returned
7Print first 100 charsdocs[0].page_contentFirst 100 characters of page content shown
💡 Loading completes after fetching, parsing, and returning the web page content as documents.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
loaderNoneWebBaseLoader instance with URLSame instanceSame instanceSame instanceSame instanceSame instance
docsNoneNoneNoneNoneNoneList with Document objectList with Document object
page_contentNoneNoneNoneRaw HTML stringParsed text stringParsed text stringParsed text string
Key Moments - 3 Insights
Why do we create a WebBaseLoader instance before calling load()?
Because the loader needs to know which URL to fetch. Step 1 shows creating the loader with the URL, so load() knows what to load.
What does the load() method return?
It returns a list of Document objects containing the parsed page content, as shown in Step 6 and Step 7.
Is the content raw HTML or parsed text when returned?
The content is parsed text extracted from the HTML, not raw HTML, as shown in Step 4 and Step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after Step 3?
AParsed text extracted from HTML
BDocument object created
CRaw HTML content received
DList of documents returned
💡 Hint
Check the 'Output/State' column for Step 3 in the execution_table.
At which step does the loader return the list of documents?
AStep 6
BStep 5
CStep 4
DStep 7
💡 Hint
Look for the step where 'Return documents' happens in the execution_table.
If the URL changes, which variable in variable_tracker changes after Step 1?
Adocs
Bloader
Cpage_content
DNone
💡 Hint
Step 1 shows creating the loader with the URL, affecting the loader variable.
Concept Snapshot
WebBaseLoader loads web pages by:
1. Creating an instance with a URL
2. Calling load() to fetch the page
3. Parsing HTML to text
4. Returning documents with page content
Use load() to get a list of Document objects containing the page text.
Full Transcript
Loading web pages with WebBaseLoader involves creating a loader instance with the target URL. When load() is called, it fetches the web page content via HTTP, parses the HTML to extract readable text, and stores it in Document objects. Finally, it returns a list of these documents. This process allows easy access to web page text for further processing.

Practice

(1/5)
1. What is the main purpose of WebBaseLoader in Langchain?
easy
A. To scrape images from websites
B. To create web pages dynamically in Python
C. To load and extract text content from web pages using a URL
D. To save web pages as PDF files

Solution

  1. Step 1: Understand WebBaseLoader's role

    WebBaseLoader is designed to fetch and extract text from web pages given their URLs.
  2. Step 2: Compare options with this role

    Only To load and extract text content from web pages using a URL describes loading and extracting text from web pages, which matches WebBaseLoader's purpose.
  3. Final Answer:

    To load and extract text content from web pages using a URL -> Option C
  4. Quick Check:

    WebBaseLoader loads web page text = D [OK]
Hint: Remember: WebBaseLoader fetches text from URLs only [OK]
Common Mistakes:
  • Thinking it creates or edits web pages
  • Confusing it with image or file download tools
  • Assuming it saves content instead of loading
2. Which of the following is the correct way to create a WebBaseLoader instance for the URL "https://example.com"?
easy
A. loader = WebBaseLoader.create("https://example.com")
B. loader = WebBaseLoader(url="https://example.com")
C. loader = WebBaseLoader.load("https://example.com")
D. loader = WebBaseLoader("https://example.com")

Solution

  1. Step 1: Recall WebBaseLoader constructor usage

    WebBaseLoader is initialized by passing the URL as a positional argument, not as a named parameter.
  2. Step 2: Check each option's syntax

    loader = WebBaseLoader("https://example.com") correctly passes the URL as a positional argument. loader = WebBaseLoader(url="https://example.com") incorrectly uses a named argument 'url'. Options C and D use methods that do not exist for instantiation.
  3. Final Answer:

    loader = WebBaseLoader("https://example.com") -> Option D
  4. Quick Check:

    Constructor takes URL as positional argument = A [OK]
Hint: Pass URL directly as first argument to WebBaseLoader [OK]
Common Mistakes:
  • Using named argument 'url' instead of positional
  • Calling load() or create() during instantiation
  • Confusing method calls with constructor
3. Given the code below, what will print(docs[0].page_content) output?
loader = WebBaseLoader("https://example.com")
docs = loader.load()
medium
A. The plain text content extracted from https://example.com
B. The raw HTML source of https://example.com
C. An error because load() returns None
D. A list of URLs found on https://example.com

Solution

  1. Step 1: Understand what load() returns

    WebBaseLoader's load() method returns a list of Document objects containing extracted plain text from the web page.
  2. Step 2: Identify what docs[0].page_content holds

    The first Document's page_content attribute holds the plain text extracted from the page, not HTML or URLs.
  3. Final Answer:

    The plain text content extracted from https://example.com -> Option A
  4. Quick Check:

    load() returns text documents = B [OK]
Hint: load() returns text documents, not HTML or URLs [OK]
Common Mistakes:
  • Expecting raw HTML instead of extracted text
  • Assuming load() returns None or errors
  • Thinking it returns links instead of content
4. What is wrong with this code snippet?
loader = WebBaseLoader(url="https://example.com")
docs = loader.load()
medium
A. You must call load_webpage() instead of load()
B. WebBaseLoader does not accept a named argument 'url' during initialization
C. load() method does not exist on WebBaseLoader
D. The URL must be a list, not a string

Solution

  1. Step 1: Check WebBaseLoader initialization parameters

    WebBaseLoader expects the URL as a positional argument, not as a named argument 'url'.
  2. Step 2: Verify method existence

    The load() method exists and is correct to call; no method named load_webpage() is required. The URL should be a string, not a list.
  3. Final Answer:

    WebBaseLoader does not accept a named argument 'url' during initialization -> Option B
  4. Quick Check:

    Named argument 'url' invalid = C [OK]
Hint: Pass URL positionally, not as named argument 'url' [OK]
Common Mistakes:
  • Using named argument 'url' instead of positional
  • Thinking load() method is missing
  • Assuming URL must be a list
5. You want to load text from multiple web pages using WebBaseLoader. Which approach correctly loads texts from two URLs and combines their content into a single string?
hard
A. loader1 = WebBaseLoader("https://site1.com") loader2 = WebBaseLoader("https://site2.com") docs = loader1.load() + loader2.load() combined_text = "".join(doc.page_content for doc in docs)
B. loader = WebBaseLoader(["https://site1.com", "https://site2.com"]) docs = loader.load() combined_text = " ".join(docs.page_content)
C. docs = WebBaseLoader.load("https://site1.com", "https://site2.com") combined_text = " ".join(doc.page_content for doc in docs)
D. loader = WebBaseLoader("https://site1.com") combined_text = loader.load("https://site2.com")

Solution

  1. Step 1: Understand WebBaseLoader usage for multiple URLs

    WebBaseLoader accepts one URL per instance. To load multiple pages, create separate loaders and load each.
  2. Step 2: Combine loaded documents correctly

    loader1 = WebBaseLoader("https://site1.com") loader2 = WebBaseLoader("https://site2.com") docs = loader1.load() + loader2.load() combined_text = "".join(doc.page_content for doc in docs) creates two loaders, loads documents from each, concatenates the lists, then joins their text content into one string.
  3. Step 3: Identify why other options fail

    loader = WebBaseLoader(["https://site1.com", "https://site2.com"]) docs = loader.load() combined_text = " ".join(docs.page_content) incorrectly passes a list of URLs to one loader (not supported). docs = WebBaseLoader.load("https://site1.com", "https://site2.com") combined_text = " ".join(doc.page_content for doc in docs) calls load as a class method with multiple URLs (invalid). loader = WebBaseLoader("https://site1.com") combined_text = loader.load("https://site2.com") tries to pass a URL to load() method (load() takes no arguments).
  4. Final Answer:

    loader1 = WebBaseLoader("https://site1.com") loader2 = WebBaseLoader("https://site2.com") docs = loader1.load() + loader2.load() combined_text = "".join(doc.page_content for doc in docs) -> Option A
  5. Quick Check:

    One loader per URL, then combine texts = A [OK]
Hint: Create one loader per URL, then merge results [OK]
Common Mistakes:
  • Passing multiple URLs to one WebBaseLoader instance
  • Calling load() with URL arguments
  • Trying to load multiple pages with one call
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard