У меня есть сценарий, возвращающий следующие результаты:
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-адресов:
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}
Это другой подход.
Он разделяет URL-адреса с text item delimiters
по разделителю &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
Результат (все URL-адреса страниц в виде списка) находится в переменной pageURLs
.
Ах, моя вина :) Хотя я должен, глядя на скрипт, просто забыл вывести правильную переменную :) Миллион благодарностей, это проще, чем мой подход!
Спасибо @vadian! Я ошибаюсь, или это "просто" возвращает URL-адрес с наибольшим целым числом? Или он должен возвращать список URL-адресов от целого 0 до maxValue?