如何从带倒计时器的span类取值?网页抓取返回None问题
Hey there! Let's dig into why your code is returning None when trying to scrape that live-updating countdown timer. Here are the most common culprits and how to fix them:
1. The Countdown is Dynamically Rendered with JavaScript
Since the timer updates every second, it's almost certainly generated or updated by client-side JavaScript. The problem? requests.get() only fetches the initial static HTML sent by the server—it doesn't execute any JavaScript. So the countdown value you're looking for doesn't even exist in the raw HTML response.
Fix: Use a Browser Automation Tool
Tools like Selenium or Playwright simulate a real browser, which runs all the page's JavaScript and loads the dynamic content. Here's a quick Selenium 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 # Initialize the browser (make sure you have ChromeDriver installed) driver = webdriver.Chrome() driver.get("your-target-url") # Wait for the countdown element to load (better than hardcoding sleep) try: countdown_element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, "span.your-countdown-class")) ) print("Current countdown:", countdown_element.text) finally: driver.quit()
2. Your Selector is Incorrect
Your code snippet cuts off at {'c...—it looks like you might have only partially written the class selector. Even a tiny typo (like missing a suffix, using id instead of class, or misspelling the attribute) will make find() return None.
Fix: Grab the Exact Selector from Dev Tools
- Open your target page in Chrome/Firefox, press F12 to open Dev Tools.
- Find the countdown
<span>element in the Elements tab. - Right-click it → Copy → Copy CSS Selector (or Copy XPath).
- Use that exact selector in your code. For example:
# If you copied a CSS selector like "span.countdown-timer" test = soup.select_one("span.countdown-timer") # Or if using XPath with Selenium countdown_element = driver.find_element(By.XPATH, "//span[@class='countdown-timer']")
3. The Countdown is Inside an Iframe
If the timer is embedded within an <iframe> tag, requests and BeautifulSoup can't access its content directly—they only see the outer page's HTML.
Fix: Access the Iframe Content
- With requests: Find the
srcattribute of the iframe, then make a separate request to that URL and scrape from there. - With Selenium: Switch to the iframe first, then locate the element:
# Switch to the iframe (use its id, class, or index if needed) iframe = driver.find_element(By.ID, "countdown-iframe") driver.switch_to.frame(iframe) # Now find the countdown element as usual countdown = driver.find_element(By.CSS_SELECTOR, "span.your-class")
4. The Site is Blocking Non-Browser Requests
Some sites detect requests from requests (which has a default user-agent that screams "I'm a bot") and return empty or modified HTML.
Fix: Spoof a Browser User-Agent
Add a User-Agent header to your requests call to mimic a real browser:
import requests from bs4 import BeautifulSoup headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" } response = requests.get("your-target-url", headers=headers) soup = BeautifulSoup(response.text, "html.parser") # Try your selector again test = soup.find("span", class_="your-full-class-name")
Start by double-checking your selector first—small mistakes here are super common! If that doesn't work, the dynamic JavaScript issue is the next most likely culprit.
内容的提问来源于stack exchange,提问作者CDNthe2nd

