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

如何用file_get_contents获取class为c-shadow的链接锚文本?

Hey there! Grabbing those anchor texts (text 1, text 2, etc.) from all links with the c-shadow class is totally doable with a few common tools—let’s go through the most practical methods based on what you’re working with:

1. Quick & Easy: Browser Developer Tools (For One-Time Checks)

If you just need to pull the texts right now without writing a full script, use your browser’s console:

  1. Open the developer tools (press F12 or Ctrl+Shift+I on Windows/Linux, Cmd+Opt+I on Mac).
  2. Navigate to the Console tab.
  3. Paste this JavaScript code and hit Enter:
    // Select all <a> elements with the 'c-shadow' class
    const targetLinks = document.querySelectorAll('a.c-shadow');
    // Convert the NodeList to an array and extract trimmed anchor texts
    const anchorTexts = Array.from(targetLinks).map(link => link.textContent.trim());
    // Log the result to the console
    console.log(anchorTexts);
    

You’ll get an array with all your desired texts (text 1, text 2, ...) that you can copy directly.

2. For Web Scraping: Python with BeautifulSoup

If you’re dealing with bulk pages or need to automate this, use Python’s BeautifulSoup library:
First, install the required packages if you haven’t already:

pip install requests beautifulsoup4

Then run this script (replace the URL with your target page):

from bs4 import BeautifulSoup
import requests

# Fetch the target webpage
url = "https://your-target-page.com"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')

# Find all links with the 'c-shadow' class
shadow_links = soup.find_all('a', class_='c-shadow')
# Extract and clean the anchor texts
anchor_texts = [link.get_text(strip=True) for link in shadow_links]

# Print or save the results
print("Extracted anchor texts:", anchor_texts)
# To save to a file:
# with open('anchor_texts.txt', 'w') as f:
#     f.write('\n'.join(anchor_texts))

3. For Frontend Projects: jQuery

If your project already uses jQuery, you can quickly gather the texts with this snippet:

const anchorTexts = [];
// Loop through each matching link and collect trimmed text
$('a.c-shadow').each(function() {
    anchorTexts.push($(this).text().trim());
});
// Use the array as needed (log to console, display on page, etc.)
console.log(anchorTexts);

Pick the method that fits your use case—all of them will reliably pull those anchor texts for you!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:11:21