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

AppleScript/Shell需求:检测URL页面内容并筛选含指定文本的URL

筛选包含指定文本的URL(页面内容检测版)

嘿,我给你准备了两种实用方案,分别用AppleScript和Shell实现,能帮你检测Single_URLs里的每个URL页面内容是否包含THIS_TEXT,自动留下符合条件的URL:

AppleScript 实现

这个脚本会遍历你的URL列表,逐个请求页面内容,检查目标文本,最后收集所有符合条件的URL。你只需要把你的20个URL填到Single_URLs列表里就行:

-- 替换成你的20个URL列表
set Single_URLs to {
    "https://example.com/page1",
    "https://example.com/page2",
    "https://teespring.com/shop/CLASSIC-DODGE-CHARGER-MOP?aid=marketplace&tsmac=marketplace&tsmic=search#pid=212&cid=5819&sid=front"
    -- 这里继续添加剩下的URL
}

set targetText to "THIS_TEXT" -- 指定要检测的文本
set matchingURLs to {}

repeat with currentURL in Single_URLs
    try
        -- 用curl请求页面,避免AppleScript原生加载的限制
        set pageContent to do shell script "curl -s -L " & quoted form of currentURL
        if pageContent contains targetText then
            set end of matchingURLs to currentURL
        end if
    on error errMsg
        -- 捕获错误,比如网络问题或无效URL,不中断整个流程
        log "处理URL [" & currentURL & "] 时出错: " & errMsg
    end try
end repeat

-- 输出结果,你可以改成写入文件或者其他操作
display dialog "符合条件的URL:" & return & joinList(matchingURLs, return)

-- 辅助函数:把列表转为字符串
on joinList(theList, delimiter)
    set AppleScript's text item delimiters to delimiter
    set joinedText to theList as string
    set AppleScript's text item delimiters to ""
    return joinedText
end joinList

Shell 脚本实现

如果你更偏好Shell环境,这个脚本同样能完成任务,语法更简洁,适合批量处理:

#!/bin/bash

# 定义你的URL数组,每个URL用引号包裹
Single_URLs=(
    "https://example.com/page1"
    "https://example.com/page2"
    "https://teespring.com/shop/CLASSIC-DODGE-CHARGER-MOP?aid=marketplace&tsmac=marketplace&tsmic=search#pid=212&cid=5819&sid=front"
    # 补充剩余URL
)

TARGET_TEXT="THIS_TEXT"
MATCHING_URLS=()

for url in "${Single_URLs[@]}"; do
    # -s 静默模式,-L 跟随重定向,--max-time 10 设置10秒超时
    if curl -s -L --max-time 10 "$url" | grep -q "$TARGET_TEXT"; then
        MATCHING_URLS+=("$url")
    fi
done

# 打印结果
echo "包含指定文本的URL:"
printf "%s\n" "${MATCHING_URLS[@]}"

小提示

  • 两个方案都用了curl,因为它能处理大多数静态页面;如果目标页面是动态渲染(比如靠JS加载内容),你可能需要用puppeteer这类工具来模拟浏览器加载,不过那需要额外安装依赖。
  • Shell脚本里的--max-time可以根据你的网络情况调整,防止单个URL请求卡住整个脚本。
  • AppleScript的错误捕获能帮你跳过无法访问的URL,保证整个遍历过程完成。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:28:47