การดึงข้อมูลจาก JavaScript
เรียนรู้การดึงข้อมูลที่สร้างหรือโหลดโดย JavaScript รวมถึงเนื้อหาจากการเรียก AJAX และแอปพลิเคชันแบบหน้าเดียว
การดึงข้อมูลจาก JavaScript เป็นบทเรียน Web Scraping & Bots ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Scraping & Bots และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Dynamic Web Content Explained
Many modern websites don't load all their content at once. Instead, they use JavaScript to fetch data and update the page after it initially loads. This is called dynamic content.
Traditional scraping tools like Requests and BeautifulSoup only see the initial HTML. They miss anything JavaScript loads later.
JavaScript's Role in Loading
JavaScript can load new data in several ways:
- AJAX Calls: Asynchronous JavaScript and XML. The browser requests data from a server in the background without reloading the entire page.
- DOM Manipulation: JavaScript directly adds, removes, or modifies elements in the page's structure (Document Object Model).
- Single-Page Applications (SPAs): Entire websites built to dynamically load content and navigate without full page refreshes.
This dynamic loading makes scraping more complex.
When Requests & BS4 Fall Short
When you use Python's requests library, you get the raw HTML that the server sends initially. If a website then uses JavaScript to load more data, that data won't be in the HTML you received.
BeautifulSoup can only parse the HTML it's given. It can't execute JavaScript to fetch additional content or wait for it to appear.
Selenium for Dynamic Content
This is where Selenium becomes essential. Selenium controls a real web browser (like Chrome or Firefox) programmatically.
When Selenium opens a page, the browser executes all JavaScript, including any AJAX calls or DOM manipulations. This means Selenium sees the fully rendered page, just like a human user would.
Waiting for Elements to Appear
Since content loads dynamically, it might not be immediately available when Selenium first loads a page. You need to tell Selenium to wait until a specific element or condition is met before trying to extract data.
If you don't wait, your script might try to find an element before JavaScript has rendered it, leading to errors or missing data.
Using Explicit Waits
Selenium's WebDriverWait combined with ExpectedConditions (EC) allows you to set explicit waits. This tells Selenium to wait for a maximum amount of time until a certain condition is true.
Common conditions include waiting for an element to be visible, clickable, or present in the DOM.
Try running this example:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
def main():
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/dynamic.html")
# Click a button that will make an element appear after a delay
driver.find_element(By.ID, "adder").click()
# Wait up to 10 seconds for the new element to appear
try:
new_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "box0"))
)
print("New element found:", new_element.text)
except Exception as e:
print("Element not found within time limit:", e)
driver.quit()
if __name__ == "__main__":
main()Extracting Rendered Data
Once you've successfully waited for dynamic content to appear, extracting the data is similar to how you'd extract from static content using Selenium.
You can use methods like find_element(By.ID, "id_name"), find_element(By.CLASS_NAME, "class_name"), or find_element(By.CSS_SELECTOR, "css_selector") to locate the elements. Then, you can retrieve their text content or attributes.
Code: Get Dynamic Text
This example shows how to wait for an element and then extract its text. Imagine a page where a "Loading..." message changes to actual data after a few seconds.
Try running this example:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
def main():
driver = webdriver.Chrome()
# Using a simple test page that changes content
driver.get("https://www.selenium.dev/selenium/web/dynamic.html")
# Click button to reveal content
driver.find_element(By.ID, "reveal").click()
# Wait for the content to be visible
try:
# The 'revealed' div becomes visible
revealed_div = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "revealed"))
)
print("Revealed content:", revealed_div.text)
except Exception as e:
print("Revealed content not found:", e)
driver.quit()
if __name__ == "__main__":
main()Scraping Single-Page Apps
Single-Page Applications (SPAs) are websites that load a single HTML page and then dynamically update content as the user interacts. Navigation within an SPA often doesn't involve full page reloads.
Selenium handles SPAs well because it acts as a full browser. You interact with elements (like clicking navigation links) and Selenium executes the JavaScript, updating the DOM. You then apply the same waiting and extraction techniques.
Quick Check on Waiting
When scraping dynamic content that appears after the initial page load, which Selenium technique is crucial to ensure the content is available before attempting to extract it?
Recap: Dynamic Data Extraction
Great job! You've learned how to tackle dynamic web content:
- Many sites use JavaScript for AJAX calls, DOM manipulation, and SPAs.
- Traditional tools like Requests and BeautifulSoup cannot execute JavaScript.
- Selenium, by controlling a full browser, can render JavaScript.
- Explicit waits are essential to ensure dynamically loaded content is present before extraction.
This skill opens up a vast number of modern websites for your scraping projects!
เรียนรู้ Python ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การดึงข้อมูลจาก JavaScript” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การดึงข้อมูลจาก JavaScript” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Scraping & Bots ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การดึงข้อมูลจาก JavaScript”
เรียนรู้การดึงข้อมูลที่สร้างหรือโหลดโดย JavaScript รวมถึงเนื้อหาจากการเรียก AJAX และแอปพลิเคชันแบบหน้าเดียว คุณปฏิบัติ Web Scraping & Bots ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Scraping & Bots หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Scraping & Bots บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การดึงข้อมูลจาก JavaScript” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web Scraping & Bots นี้ได้ไหม
ได้ บทเรียน Web Scraping & Bots ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ Selenium
- การทำให้การโต้ตอบกับเบราว์เซอร์เป็นอัตโนมัติ
- การดึงข้อมูลจาก JavaScript
- กลยุทธ์การรอสำหรับหน้าเว็บแบบไดนามิก