与此作斗争:
这就是我现在所拥有的,无法找到一个工作的解决方案(除了在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))每一个帮助都是非常感谢的。谢谢
更新:
--
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的代码进行跟踪:
发布于 2018-05-05 19:33:33
为了从具有类image_stack__image的元素中获取所有图像URL(假设该类的元素是<img>元素,并满足所有共享这个类名的多个图像),这一行JavaScript将返回一个src属性值数组:
Array.from(document.querySelectorAll('.image_stack__image'), e=>e.src)在Safari中使用AppleScript命令时,do JavaScript将自动将其转换为列表。
若要将所有URL都保存到主文件夹中的目录cURL中,并将每个图像保存在与远程文件相同的名称下,首先将cd保存到目录中,然后使用--remote-name-all选项进行cURL:
cd ~/thumbnails; curl --remote-name-all %url1% %url2% ...警告:可能不会下载带有异常URL的映像,例如那些通过CGI请求动态生成的映像,或者其src属性包含base64 64编码数据的图像。事实上,在curl请求中存在这些可能会破坏整个请求。
要连接从JavaScript方法返回的URL列表,以便您可以将其直接绑定到cURL中,只需使用space作为分隔符将AppleScript列表强制放入text:
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选项,并发现许多您可能认为有益的其他选项。
但是我会尽我最大的努力来帮助你遇到任何与我所写的相关的小的道路颠簸或疑问。
https://stackoverflow.com/questions/50175791
复制相似问题