首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >苹果-或者Shellscript -被攻击。图片的"src“的类名,并下载

问苹果-或者Shellscript -被攻击。图片的"src“的类名,并下载
EN

Stack Overflow用户
提问于 2018-05-04 13:10:38
回答 1查看 322关注 0票数 0

与此作斗争:

  1. 循环遍历包含url's的字符串的var All_URLs
  2. 使用"image_stack__image js-default-img“类从图像中获取所有src属性。
  3. 将所有图像下载到文件夹中,并使用源页面中的URL作为img名称。

这就是我现在所拥有的,无法找到一个工作的解决方案(除了在Automator中的一个动作),它的工作方式是预期的。

代码语言:javascript
复制
    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))

每一个帮助都是非常感谢的。谢谢

更新:

  1. 设法从包含所需类的映像中获取src/url。
  2. 设法将其下载到所需的文件夹。
  3. 需要将保存的映像名设置为来自.的源url。
  4. 需要在一个循环中完成所有这一切,因为我将拥有不同的urls,而不仅仅是一个urls,例如.。

--

代码语言:javascript
复制
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的代码进行跟踪:

  1. "cd ~/缩略图;“x确实保存到桌面文件夹”缩略图“缩略图。我需要脚本文件夹的相对路径,以防用户移动文件夹。没有为它找到一个卷曲解决方案,而是一个工作的”告诉应用程序查找器“(line1)。
  2. 下载的文件在上一个/ (560.jpg)之后具有相同的结尾,我尝试使用"set My_Name to do shell脚本“"uuidgen”并将其添加到sh中,但我更希望将文件命名为1.jpg、2.jpg等。 告诉应用程序"Finder“--让filepath将容器/文件夹集myPath设置为文本容器--设置主路径结束,将allURL设置为{"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"}将JS设置为”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"}将JS设置为{"cd ~/桌面/缩略图”;"curl --远程名称-all“,{}--需要将位置设置为脚本的主文件夹,文件名为1.jpg,2.jjpg。将文本项分隔符设置为空格告知应用程序"Safari“以在allURL中使用www重复设置为D(创建具有属性{URL:www}的新文档)#等网页加载了tell D以重复,直到未(存在)延迟0.5结束重复设置sh的最后一项在前面的文档中完成JavaScript JS关闭前端文档do shell脚本(sh作为文本) 结束重复
EN

回答 1

Stack Overflow用户

发布于 2018-05-05 19:33:33

为了从具有类image_stack__image的元素中获取所有图像URL(假设该类的元素是<img>元素,并满足所有共享这个类名的多个图像),这一行JavaScript将返回一个src属性值数组:

代码语言:javascript
复制
Array.from(document.querySelectorAll('.image_stack__image'), e=>e.src)

在Safari中使用AppleScript命令时,do JavaScript将自动将其转换为列表。

若要将所有URL都保存到主文件夹中的目录cURL中,并将每个图像保存在与远程文件相同的名称下,首先将cd保存到目录中,然后使用--remote-name-all选项进行cURL:

代码语言:javascript
复制
cd ~/thumbnails; curl --remote-name-all %url1% %url2% ...

警告:可能不会下载带有异常URL的映像,例如那些通过CGI请求动态生成的映像,或者其src属性包含base64 64编码数据的图像。事实上,在curl请求中存在这些可能会破坏整个请求。

要连接从JavaScript方法返回的URL列表,以便您可以将其直接绑定到cURL中,只需使用space作为分隔符将AppleScript列表强制放入text:

代码语言:javascript
复制
    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重复完全相同的过程:

代码语言:javascript
复制
    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选项,并发现许多您可能认为有益的其他选项。

但是我会尽我最大的努力来帮助你遇到任何与我所写的相关的小的道路颠簸或疑问。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50175791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档