0Pricing
AI Agents with LangChain & Autonomous Workflows · บทเรียน

การดึงข้อมูลจากเว็บและการเสริมข้อมูล

นำเทคนิคที่ช่วยให้เอเจนต์ดึงข้อมูลจากเว็บไซต์และเพิ่มพูนฐานความรู้แบบไดนามิกมาใช้

การดึงข้อมูลจากเว็บและการเสริมข้อมูล เป็นบทเรียน AI Agents with LangChain & Autonomous Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Agents with LangChain & Autonomous Workflows และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Agents with LangChain & Autonomous Workflows มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Web Scraping for Agents

AI agents often need up-to-date, specific information that isn't part of their pre-trained knowledge or available via simple API calls.

This is where web scraping comes in! It allows agents to "read" and extract data directly from web pages, just like a human would.

What is Web Scraping?

Web scraping is the automated process of extracting data from websites. Instead of manually copying information, a program does it for you.

For AI agents, this means they can dynamically gather facts, prices, news, or any other publicly available information from the internet to inform their decisions or responses.

Essential Python Libraries

Python has powerful libraries that make web scraping straightforward:

  • requests: Used to make HTTP requests (like visiting a website) and get the raw HTML content.
  • BeautifulSoup (often imported as bs4): A library for parsing HTML and XML documents, making it easy to extract data.

Together, they form a common duo for scraping tasks.

Getting Web Page Content

First, we use the requests library to fetch the content of a web page. This sends a request to the server and gets the HTML response.

Try running this example to see how we fetch the content from example.com:

import requests

def main():
    try:
        # Send a GET request to the URL
        response = requests.get("https://www.example.com")
        
        # Check if the request was successful (status code 200)
        if response.status_code == 200:
            print(f"Successfully fetched example.com!")
            print(f"Content Length: {len(response.text)} characters")
        else:
            print(f"Failed to fetch. Status Code: {response.status_code}")
    except requests.exceptions.RequestException as e:
        print(f"Error fetching URL: {e}")

if __name__ == "__main__":
    main()

HTML Basics for Scraping

Web pages are built with HTML (HyperText Markup Language). HTML uses tags like <h1>, <p>, <a> to structure content.

BeautifulSoup helps us navigate this structure. To extract data, we need to know what tags, classes, or IDs the information is wrapped in.

Parsing with BeautifulSoup

Once you have the raw HTML, BeautifulSoup turns it into a parse tree, allowing you to search for elements easily.

Here's how to create a BeautifulSoup object and extract a simple title from a string:

from bs4 import BeautifulSoup

def main():
    html_doc = """
    <html><head><title>CoddyKit Lesson</title></head>
    <body>
    <p class="intro"><b>Hello Learners!</b></p>
    </body></html>
    """
    
    # Create a BeautifulSoup object
    soup = BeautifulSoup(html_doc, 'html.parser')
    
    # Access the title tag and its string content
    print(f"Page Title: {soup.title.string}")

if __name__ == "__main__":
    main()

Finding Specific Data

BeautifulSoup offers powerful methods like find() (for the first match) and find_all() (for all matches) to locate elements by tag name, attributes, or CSS selectors.

Let's find the text inside an <h1> tag:

from bs4 import BeautifulSoup

def main():
    html_doc = """
    <html>
    <body>
        <h1>Welcome to Our Course</h1>
        <p>This is an example paragraph.</p>
        <div id="footer">Contact Us</div>
    </body>
    </html>
    """
    soup = BeautifulSoup(html_doc, 'html.parser')
    
    # Find the first h1 tag
    first_h1 = soup.find('h1')
    if first_h1:
        print(f"Extracted H1 Text: {first_h1.get_text()}")
    else:
        print("H1 tag not found.")

if __name__ == "__main__":
    main()

Data Augmentation for Agents

Data augmentation, in this context, refers to the process of enhancing an agent's internal knowledge base or current context with newly acquired information.

When an agent scrapes a website, it's not just "reading" for itself; it's gathering data that can be used to improve its understanding, answer questions, or make more informed decisions.

Enhancing Agent Capabilities

Imagine an agent that needs to provide real-time stock prices or the latest news headlines. Its pre-trained model won't have this info.

  • Real-time data: Scrape current stock prices or news.
  • Specific facts: Extract product details from an e-commerce site.
  • Contextual understanding: Get background info on a topic not covered in its training.

This scraped data can then be passed to the LLM as part of the prompt, augmenting its knowledge.

Ethical & Legal Considerations

When scraping, always be mindful of:

  • robots.txt: A file on websites that tells bots which parts of the site they shouldn't access. Respect it!
  • Terms of Service: Many sites prohibit scraping in their terms.
  • Rate Limiting: Don't bombard a server with too many requests too quickly; it can be seen as a DoS attack. Introduce delays.

Scrape responsibly and ethically.

Quick Check

You've learned about fetching web content and parsing HTML. Which Python library is primarily used for navigating and extracting data from HTML documents?

Recap & Next Steps

In this lesson, you learned how to use Python's requests and BeautifulSoup libraries for web scraping. We covered fetching raw HTML and then parsing it to extract specific information.

You also understood how this scraped data contributes to data augmentation, enabling AI agents to access current and specific information, thereby enriching their knowledge and capabilities dynamically. Always remember to scrape responsibly!

คำถามที่พบบ่อย

บทเรียน “การดึงข้อมูลจากเว็บและการเสริมข้อมูล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การดึงข้อมูลจากเว็บและการเสริมข้อมูล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Agents with LangChain & Autonomous Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Agents with LangChain & Autonomous Workflows มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การดึงข้อมูลจากเว็บและการเสริมข้อมูล”

นำเทคนิคที่ช่วยให้เอเจนต์ดึงข้อมูลจากเว็บไซต์และเพิ่มพูนฐานความรู้แบบไดนามิกมาใช้ คุณปฏิบัติ AI Agents with LangChain & Autonomous Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Agents with LangChain & Autonomous Workflows หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Agents with LangChain & Autonomous Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การดึงข้อมูลจากเว็บและการเสริมข้อมูล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน AI Agents with LangChain & Autonomous Workflows นี้ได้ไหม

ได้ บทเรียน AI Agents with LangChain & Autonomous Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การสร้างเครื่องมือ LangChain แบบกำหนดเอง
  2. การผสานรวมกับ API ภายนอก
  3. การดึงข้อมูลจากเว็บและการเสริมข้อมูล
  4. ชุดเครื่องมือและข้อมูลนำเข้าเครื่องมือแบบมีโครงสร้าง
← กลับไปที่ AI Agents with LangChain & Autonomous Workflows