0

我需要递归地镜像一些具有特定标记的站点壁纸图像,例如:

<div class="wb_more">
Original Resolution: <a href="//site.com/download/space_planet_sky_94434/4800x2700">4800x2700</a><br>
Views: <a href="/download/last">96661</a>
</div>

但不是其他人,例如:

<div class="wd_resolution">
<span class="wd_res_cat">Fullscreen</span>
<span class="wd_res_cat_raz"><a class="wb_res_select" href="//site.com/download/space_planet_sky_94434/1600x1200">1600x1200</a>
...
</span>
...
</span>
</div>

请注意,URL 是相同的,除了分辨率,但原件的分辨率可能会有所不同,所以只有周围的标记会有所不同,比如在链接前面加上Original Resolution:.

有没有使用 wget 或 httrack 或其他工具的解决方案?

谢谢你。

4

2 回答 2

2

您可以使用像scrapy这样的抓取工具来做到这一点。您可以使用 css、xpath、regex 或其他方式解析 html 响应,以获取与您的规则匹配的链接。

我认为最好为每个站点制作刮板。例如,对于第一个:

import scrapy

class imageLink(scrapy.Spider):
    name = 'imageLink'
    
    # Here the list of url to start scraping
    start_urls = ['https://images.com']

    def parse(self, response):
        # Get the link
        link = response.css('div.wb_more > a ').xpath('@href')
        # Make a callback to save the image
        yield scrapy.Request(url=url, callback=self.save_image)
   
    def save_image(self, response):
        link = response.url
        # Guess the filename from the link
        # space_planet_sky_94434
        filename = link.split('/')[5]
        # Save the image
        with open(filename, 'wb') as f:
            f.write(response.body)

如果网站有图像分页,您可以添加回调以解析下一页的链接。

我没有测试代码。

于 2017-08-03T17:52:48.010 回答
1

您可以尝试使用普通wget并在其上使用正则表达式(使用sedperl例如)然后下载您获得的链接(wget可以做到)

一个基本的脚本看起来像这样

wget [URL] -o MyPage.html
./GetTheFlag.pl -html MyPage.html > WallPaperList.txt
wget -i WallPaperList.txt #here you can put your image where you want

用 GetFlag.pl 看起来像

use warnings; 
use strict; 
use Getopt::Long;
my $Url;
my $MyPage;
GetOptions("html=s" => \$MyPage);
open(FILE,$MyPage);
my $content=FILE;
my @urls=($content =~ //gi) #insert here a regex depending on the flags around
foreach my $i (@urls) {
    print "$i\n";
}

例如,如果您的网址是<a href="url">New Wallpaper</a>正则表达式将是

 =~ /<a href="(\w+)">New Wallpaper</a>

关心\w它错过了一些不能在 var name 中使用的字符-

希望这足够清楚。

于 2017-08-03T17:31:25.523 回答