与此作斗争:
- 循环遍历包含 url 字符串的 var All_URLs
- 从类为“image_stack__image js-default-img”的图像中获取所有 src 属性
- 将所有图像下载到一个文件夹并使用源页面中的 URL 作为图像名称。
这就是我现在所拥有的一切,但无法找到按预期工作的有效解决方案(除了 Automator 中的操作)。
tell application "Finder"
set myPath to container of (path to me) as text -- SET MAIN PATH
end tell
set AllUrls to {"https://teespring.com/shop/CLASSIC-DODGE-CHARGER-MOP?aid=marketplace&tsmac=marketplace&tsmic=search#pid=212&cid=5819&sid=front", "https://teespring.com/shop/greaser-mechanics-t-shirt?aid=marketplace&tsmac=marketplace&tsmic=campaign#pid=2&cid=2397&sid=front"}
--set ImageSrc to (script to get the src attribute from the class "image_stack__image js-default-img"
--set IMGname to the Page URL where the image is
set dFolder to myPath & "thumbnails"
set fName to IMGname & ".jpg" as string
do shell script ("mkdir -p " & dFolder & "; curl -A/--user-agent " & AllUrls & " >> " & (dFolder & fName))
我们非常感谢您的每一次帮助。谢谢
更新:
- 设法从包含该类的图像中获取 src/url 需要。
- 成功将其下载到所需的文件夹。
- 需要将保存的图片名称设置为图片所在的源url 来自。
- 需要循环执行所有这些操作,因为我将有不同的网址和 不仅仅是示例中的一个。
--
set home_path to (((path to me as text) & "::") as alias) as string
tell application "Safari"
open location "https://teespring.com/shop/CLASSIC-DODGE-CHARGER-MOP?aid=marketplace&tsmac=marketplace&tsmic=search#pid=212&cid=5819&sid=front"
set campaign_thumbnail to do JavaScript "document.querySelector('.image_stack__image').src" in document 1
end tell
do shell script "curl -f " & quoted form of campaign_thumbnail & " -o " & quoted form of (POSIX path of home_path) & "thumbnails/test.jpg"
更新 2:
遵循 CJK 代码:
- “cd ~/缩略图;” |是否保存到桌面文件夹“缩略图。我需要相对路径 脚本文件夹,以防用户移动该文件夹。没有找到一个 curl 解决方案,但“告诉应用程序查找器”确实有效 (第 1 行)
下载的文件在最后一个/(560.jpg) 后具有相同的结尾 我尝试使用“set My_Name to do shell script”uuidgen”并且 将其添加到 sh.但我宁愿需要将文件命名为 1.jpg , 2.jpg等。
tell application "Finder" -- get filepath to file container/folder set myPath to container of (path to me) as text -- SET MAIN PATH end tell set allURLs to {"https://teespring.com/shop/CLASSIC-DODGE-CHARGER-MOP?aid=marketplace&tsmac=marketplace&tsmic=search#pid=212&cid=5819&sid=front", "https://teespring.com/shop/dodge-mopar-m?aid=marketplace&tsmac=marketplace&tsmic=search#pid=2&cid=2397&sid=front"} set JS to "document.querySelector('.image_stack__image').src" set sh to {"cd ~/desktop/thumbnails;", "curl --remote-name-all ", {}} -- need to set the location to the home folder of the script and the filename to 1.jpg , 2.jpg .. set the text item delimiters to space tell application "Safari" to repeat with www in allURLs set D to (make new document with properties {URL:www}) # Wait until webpage has loaded tell D to repeat until not (exists) delay 0.5 end repeat set the last item of sh to do JavaScript JS in the front document close the front document do shell script (sh as text)
结束重复
最佳答案
从类 image_stack__image
的元素中获取所有图像 URL (假设此类的元素是 <img>
元素,并满足共享此类名称的多个图像)这行 JavaScript 将返回 src
的数组属性值:
Array.from(document.querySelectorAll('.image_stack__image'), e=>e.src)
当您使用 do JavaScript
时,AppleScript 会自动将其转换为列表。 Safari 中的命令。
致cURL
目录 "thumbnails"
中的所有 URL在您的主文件夹中,并将每个图像保存在与远程文件相同的名称下,cd
先进入目录,然后cURL
使用--remote-name-all
选项:
cd ~/thumbnails; curl --remote-name-all %url1% %url2% ...
警告:可能不会下载具有异常 URL 的图像,例如通过 CGI 请求动态生成的图像,或 src
的图像。属性包含 base64 编码的数据。事实上,这些都存在于curl
中。请求可能会中断整个请求。
连接从 JavaScript 方法返回的 URL 列表,以便您可以将其直接插入 cURL
,只需将 AppleScript 列表强制为 text
使用space
作为分隔符:
set JS to "Array.from(document.querySelectorAll('.image_stack__image'), e=>e.src);"
set sh to {"cd ~/thumbnails;", "curl --remote-name-all"}
set the text item delimiters to space
tell application "Safari" to tell ¬
the front document to set ¬
the end of sh to ¬
do JavaScript JS
do shell script (sh as text)
然后,通过将适当的代码行包含在 repeat
中,对每个网页 URL 重复完全相同的过程。循环:
set allURLs to {%your list of URLs%}
set JS to "Array.from(document.querySelectorAll('.image_stack__image'),e=>e.src);"
set sh to {"cd ~/thumbnails;", "curl --remote-name-all", {}}
set the text item delimiters to space
tell application "Safari" to repeat with www in allURLs
set D to (make new document with properties {URL:www})
# Wait until webpage has loaded
tell D to repeat until not (exists)
delay 0.5
end repeat
set the last item of sh to do JavaScript JS in the front document
close the front document
do shell script (sh as text)
end repeat
这就是它的基本原理。在 URL 格式不寻常或网页无法加载等情况下,您需要注意错误处理,但您现在拥有完成您请求的步骤的所有工具。
此外,我建议阅读 curl
的联机帮助页(在终端中输入man curl
),并阅读有关--remote-name-all
的信息选项并发现许多您可能会发现有益的其他选项。
但我会尽力帮助您解决遇到的任何小问题或与我所写内容相关的疑问。
关于javascript - Apple 或 Shellscript |获取属性。图像的类名的 "src"并下载它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50175791/