applescript - 苹果脚本 | 提取最大整数并创建从0到最大整数的数字
问题描述
我有一个脚本返回以下结果:
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
现在我需要找到最高整数(在本例中为 &p=29)并从https://XY.com/search?q=mad%20dog&p=0创建新字符串,直到在本例中找到的最高整数https:/ /XY.com/search?q=mad%20dog&p=29
到目前为止,我设法使用以下代码提取了所需的 URL:
set AllUrls to {"https://XY.com/search?q=mad%20dog&p=26", "https://XY.com/search?q=mad%20dog&p=29", "https://XY.com/shop/General-Mad-Dog-Mattis-For-PrN"}
-- FILTER PAGING URLS
set PagingFilter to "&p="
set PagingUrls to {}
repeat with i from 1 to length of AllUrls
if item i of AllUrls contains PagingFilter then
set end of PagingUrls to item i of AllUrls
end if
end repeat
PagingUrls -- returns {"https://XY.com/search?q=mad%20dog&p=26", "https://XY.com/search?q=mad%20dog&p=29"}
还有一个从 URL 中提取最后 2 位数字的小脚本:
set alphabet to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set myURLs to {"https://XY.com/search?q=mad%20dog&p=26", "https://XY.com/search?q=mad%20dog&p=29"}
set the text item delimiters of AppleScript to ¬
{space} & characters of the alphabet & {".", "_"}
set a to text items of myURLs as text
get last word of a --> returns "21"
set numlist to {}
repeat with i from 1 to count of words in a
set this_item to word i of a
try
set this_item to this_item as number
set the end of numlist to this_item
end try
end repeat
numlist -- returns {26, 29}
解决方案
这是另一种方法。
text item delimiters
它使用分隔符分隔URL &p=
。如果分隔符存在,则获取整数(分隔符的右侧)并将其保存,就maxValue
好像当前值高于前一个值一样。
然后使用循环创建页面 URL 列表
set AllUrls to {"https://XY.com/search?q=mad%20dog&p=26", "https://XY.com/search?q=mad%20dog&p=29", "https://XY.com/shop/General-Mad-Dog-Mattis-For-PrN"}
set maxValue to 0
set baseURL to ""
set TID to text item delimiters
set text item delimiters to "&p="
repeat with anURL in AllUrls
set textItems to text items of anURL
if (count textItems) is 2 then
set currentValue to item 2 of textItems as integer
if currentValue > maxValue then set maxValue to currentValue
set baseURL to item 1 of textItems & "&p="
end if
end repeat
set text item delimiters to TID
set pageURLs to {}
repeat with i from 0 to maxValue
set end of pageURLs to baseURL & i
end repeat
推荐阅读
- git - How to pull some files from an old commit to the current master
- javascript - 如何使用jQuery检查鼠标是否远离元素?
- postgresql - PostgreSQL TO_CHAR() 删除月份字段的填充
- list - 按特定值从通用列表中获取所有项目
- python - 仅当列列表包含来自另一个列表的值时过滤行
- json - Import ndjson into elasticsearch 7.2 by command line
- php - Sort "orders" in batch of 4, based on most common products ordered
- oracle - PL/SQL,查找Oracle SQL中执行文件的当前目录
- node.js - Express docker应用程序上的连接被拒绝
- angular - 如何从服务中的另一个服务订阅 BehaviorSubject?