AppleScript加密货币价格爬取脚本失效,仅输出名称与时间求助
问题排查与解决方案
核心问题原因
- CoinMarketCap网站HTML结构已更新,原脚本依赖的
priceValue类名及价格元素格式已不存在,正则表达式无法匹配到价格内容 - 直接使用
curl请求可能触发网站基础反爬机制,返回验证页面而非实际价格数据
排查步骤
验证curl请求结果:在终端执行以下命令,查看返回内容是否为正常币种页面:
curl --no-keepalive 'https://coinmarketcap.com/currencies/bitcoin/'如果返回Cloudflare验证页面或无价格相关文本,说明反爬拦截或页面结构变更导致失效。
检查当前页面价格元素:打开对应币种页面,右键价格区域选择「检查」,查看价格元素的最新结构。目前价格通常直接以
$X,XXX.XX格式显示在页面中,无需依赖特定类名匹配。
解决方案
方案1:更新脚本适配新页面结构(规避反爬+新正则)
修改后的脚本添加浏览器请求标识,更新正则匹配规则,适配当前页面的价格展示:
property eGrepBitcoinPrice : "\\$\\d{1,3},\\d{3}\\.\\d{2}" property eGrepLitecoinPrice : "\\$\\d{1,3}\\.\\d{2}" property eGrepDogecoinPrice : "\\$\\d{0,2}\\.\\d{5}" property currentBitcoinPrice : missing value property currentLitecoinPrice : missing value property currentDogecoinPrice : missing value property logToTextFile : missing value on run -- 仅在脚本小程序启动时执行一次 activate set logToTextFile to (display dialog ¬ "启用快速日志模式?" buttons {"否", "是"} ¬ default button 2 with title "日志模式") if button returned of logToTextFile = "是" then my logCommands's beginLog() getPrices() else getPrices() return {currentBitcoinPrice, currentDogecoinPrice, currentLitecoinPrice} end if end run on idle getPrices() try if button returned of logToTextFile = "是" then my logCommands's writeToLog() on error errMsg number errNum my logCommands's writeToLog() end try return 5 -- 此idle处理程序的运行间隔(秒) end idle on getPrices() -- 添加User-Agent模拟浏览器请求,规避基础反爬 set userAgent to "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" -- 比特币请求改为主页面(结构更简单) set currentBitcoinPrice to do shell script ¬ "curl --no-keepalive -A '" & userAgent & "' 'https://coinmarketcap.com/currencies/bitcoin/' " & ¬ "| grep -Eo " & quoted form of eGrepBitcoinPrice & " | head -n 1" set currentLitecoinPrice to do shell script ¬ "curl --no-keepalive -A '" & userAgent & "' 'https://coinmarketcap.com/currencies/litecoin/' " & ¬ "| grep -Eo " & quoted form of eGrepLitecoinPrice & " | head -n 1" set currentDogecoinPrice to do shell script ¬ "curl --no-keepalive -A '" & userAgent & "' 'https://coinmarketcap.com/currencies/dogecoin/' " & ¬ "| grep -Eo " & quoted form of eGrepDogecoinPrice & " | head -n 1" end getPrices on quit -- 仅在脚本退出时执行 if button returned of logToTextFile = "是" then my logCommands's endLog() continue quit -- 允许脚本退出 end quit script logCommands property pathToPriceLog : POSIX path of (path to desktop as text) & "价格日志.txt" on beginLog() set startTime to ("开始时间... " & (current date) as text) & ¬ " 每5秒扫描一次价格" do shell script "echo " & quoted form of startTime & " >> " & ¬ quoted form of pathToPriceLog end beginLog on writeToLog() do shell script "echo " & quoted form of ("比特币:" & currentBitcoinPrice & ¬ " 狗狗币:" & currentDogecoinPrice & ¬ " 莱特币:" & currentLitecoinPrice & ¬ " " & (time string of (current date))) & ¬ " >> " & quoted form of pathToPriceLog end writeToLog on endLog() set endTime to "结束时间... " & (current date) as text do shell script "echo " & quoted form of endTime & " >> " & ¬ quoted form of pathToPriceLog do shell script "echo '' >> " & ¬ quoted form of pathToPriceLog end endLog end script
关键修改点说明
- 添加
-A参数设置浏览器User-Agent,避免被基础反爬拦截 - 简化正则表达式:直接匹配价格的货币符号+数字格式,不再依赖已被网站移除的
priceValue类名 - 将比特币请求从
/markets/页面改为主币种页面,结构更稳定,价格更容易匹配 - 修复日志写入的引号拼接问题,避免字符串语法错误
方案2:使用CoinMarketCap官方API(更稳定)
网页爬取易受页面结构变更影响,推荐使用官方API获取数据(需注册免费API密钥),示例框架:
property apiKey : "你的API密钥" property currentBitcoinPrice : missing value property currentLitecoinPrice : missing value property currentDogecoinPrice : missing value on getPrices() -- 批量获取BTC、LTC、DOGE的最新价格 set apiResponse to do shell script ¬ "curl -H 'X-CMC_PRO_API_KEY: " & apiKey & "' 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=BTC,LTC,DOGE'" -- 解析JSON响应(可使用AppleScript原生JSON支持或第三方工具如JSON Helper) -- 示例解析逻辑(需适配实际JSON结构) set currentBitcoinPrice to 从JSON响应中提取BTC的USD价格字段 set currentLitecoinPrice to 从JSON响应中提取LTC的USD价格字段 set currentDogecoinPrice to 从JSON响应中提取DOGE的USD价格字段 end getPrices
注:官方API免费版有请求次数限制,适合低频率获取需求。
内容的提问来源于stack exchange,提问作者TOKI
相关产品推荐
相关产品推荐

