AppleScript开发:提取最大整数并生成0至该整数的序列URL
解决方案:提取最大页码并生成连续搜索URL
从你给出的代码片段和需求来看,咱们用AppleScript就能轻松搞定这个任务,步骤拆解如下:
1. 先定义你的URL列表
把你提到的目标URL先整理成列表:
set AllUrls to {¬ "https://XY.com/shop/General-Mad-Dog-Mattis-For-PrN", ¬ "https://XY.com/search?q=mad%20dog&p=29", ¬ "https://XY.com/search?q=mad%20dog&p=26" ¬ }
2. 筛选搜索URL并提取页码
先把带&p=的搜索URL挑出来,再从中提取页码数字:
set pageNumbers to {} repeat with aUrl in AllUrls if aUrl contains "&p=" then set AppleScript's text item delimiters to "&p=" set pageText to text item 2 of aUrl set AppleScript's text item delimiters to "" try set pageNum to pageText as integer copy pageNum to end of pageNumbers end try end if end repeat
3. 找出最大页码值
从提取到的页码里筛选出最大的那个:
set maxPage to 0 repeat with num in pageNumbers if num > maxPage then set maxPage to num end repeat
4. 生成完整的连续URL列表
最后从p=0到p=maxPage循环生成所有目标URL:
set baseSearchUrl to "https://XY.com/search?q=mad%20dog&p=" set newUrlList to {} repeat with i from 0 to maxPage copy (baseSearchUrl & i) to end of newUrlList end repeat -- 可以打印结果验证 log newUrlList
小说明
- 第一步自动过滤了商品详情页(第一个URL),只处理带页码参数的搜索页
- 用文本分隔符拆分URL提取页码,再转成整数,避免了字符串比较可能出现的错误
- 循环生成URL时直接复用基础搜索链接,保证格式统一
内容的提问来源于stack exchange,提问作者GTO
相关产品推荐
相关产品推荐

