【问题标题】:Download images from website从网站下载图片
【发布时间】:2012-05-04 04:45:17
【问题描述】:

我想在网站上拥有画廊的本地副本。图库在 domain.com/id/1 显示图片(id 以 1 为增量增加),然后图像存储在 pics.domain.com/pics/original/image.format。图像在 HTML 中的确切行是

<div id="bigwall" class="right"> 
    <img border=0 src='http://pics.domain.com/pics/original/image.jpg' name='pic' alt='' style='top: 0px; left: 0px; margin-top: 50px; height: 85%;'> 
</div>

所以我想写一个脚本来做这样的事情(在伪代码中):

for(id = 1; id <= 151468; id++) {
     page = "http://domain.com/id/" + id.toString();
     src = returnSrc(); // Searches the html for img with name='pic' and saves the image location as a string
     getImg(); // Downloads the file named in src
}

不过,我不确定该怎么做。我想我可以在 bash 中执行此操作,使用 wget 下载 html,然后手动在 html 中搜索 http://pics.domain.com/pics/original/. 然后再次使用 wget 保存文件,删除 html 文件,增加 id并重复。唯一的问题是我不擅长处理字符串,所以如果有人能告诉我如何搜索 url 并将 *s 替换为文件名和格式,我应该能够完成其余的工作。或者,如果我的方法很愚蠢,而您有更好的方法,请分享。

【问题讨论】:

    标签: image bash wget


    【解决方案1】:
    # get all pages
    curl 'http://domain.com/id/[1-151468]' -o '#1.html'
    
    # get all images
    grep -oh 'http://pics.domain.com/pics/original/.*jpg' *.html >urls.txt
    
    # download all images
    sort -u urls.txt | wget -i-
    

    【讨论】:

    • 谢谢!优雅、快速且易于理解的解决方案。
    • @Mike 我没有测试这些命令。如果您有任何问题,请告诉我。
    • 使用 grep -oh 删除 url 之前的文件名。
    • 太棒了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    相关资源
    最近更新 更多