You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python爬虫:如何解析指定div中的所有链接(附代码示例)

No problem! To pull all links from that <div> tag, Python's BeautifulSoup library is your best bet—it makes parsing HTML a breeze. Here's a step-by-step breakdown:

Step 1: Set Up Your Tools

First, make sure you have BeautifulSoup installed (run pip install beautifulsoup4 if you don’t). If you’re scraping a live webpage instead of working with a static HTML snippet, you’ll also need the requests library (pip install requests).

Step 2: Parse the HTML & Target the Div

Whether you’re using saved HTML or fetching a live page, here’s how to isolate the div and extract its links:

Example with Static HTML Snippet

from bs4 import BeautifulSoup

# Your provided HTML content
html_content = '''<div class="RadAjaxPanel" id="LiveBoard1_LiveBoard1_litGamesPanel"> <a href="leaders.aspx?pos=all&stats=pit&lg=all&qual=0&type=8&season=2016&month=0&season1=2016&ind=0&team=0&rost=0&age=0&filter=&players=p2018-04-20"> Today's Probable Starters and Lineups Leaderboard </a> </div>'''

# Parse the HTML
soup = BeautifulSoup(html_content, 'html.parser')

# Find the target div using its unique ID (more reliable than class)
target_div = soup.find('div', id='LiveBoard1_LiveBoard1_litGamesPanel')

# Extract all <a> tags inside the div
if target_div:
    links = target_div.find_all('a')
    
    # Loop through links to get URL and clean text
    for link in links:
        link_url = link.get('href')
        link_text = link.get_text(strip=True)
        print(f"Link Text: {link_text}\nURL: {link_url}\n")
else:
    print("Target div not found in the HTML.")

Example with a Live Webpage

If you’re scraping this from a live site, first fetch the page content with requests:

import requests
from bs4 import BeautifulSoup

# Replace with the actual URL you're scraping
url = "https://your-target-site.com/page"
response = requests.get(url)

# Check if the request succeeded
if response.status_code == 200:
    soup = BeautifulSoup(response.text, 'html.parser')
    target_div = soup.find('div', id='LiveBoard1_LiveBoard1_litGamesPanel')
    
    if target_div:
        links = target_div.find_all('a')
        for link in links:
            print(f"Text: {link.get_text(strip=True)}\nURL: {link.get('href')}\n")
    else:
        print("Couldn’t locate the target div—maybe the page structure changed?")
else:
    print(f"Failed to fetch the page: Status code {response.status_code}")

Quick Tips

  • Using the div’s ID is better than its class because IDs are unique in HTML (classes can be reused across multiple elements).
  • strip=True in get_text() removes extra whitespace from the link text for cleaner output.
  • Always add checks (like verifying the div exists) to avoid crashes if the HTML structure changes unexpectedly.

内容的提问来源于stack exchange,提问作者Nate Walker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:35:02