4

我想要一个可以提取*.jpg网页上所有图像(例如)列表的脚本,即以.jpg

使用此脚本,我将使用管道输出到文件>,然后使用管道输出作为wget.

这可以使用shell脚本吗?

(编辑:我正在使用bash外壳)

4

3 回答 3

3

怎么用lynx

lynx -image_links -dump www.google.com |
    grep '\. https\?://.*\.\(gif\|jpg\|png\)$'

要稍微清理一下输出,您可以使用cut

lynx -image_links -dump www.google.com |
    grep '\. https\?://.*\.\(gif\|jpg\|png\)$' |
    cut -d . -f 2- |
    cut -d ' ' -f 2-
于 2012-08-25T21:37:32.883 回答
0

因为并不是所有的 shell 都提供开箱即用的 Web 连接(Windows shell 肯定没有),所以动态脚本语言(如 Ruby 或 Python)的解决方案可以跨平台工作。

下面是一些 Ruby 代码,当指向网页时,会在其中搜索 *.jpg,将它们全部列出,然后下载它们。(顺便说一句,它可以用于 PDF、PNG,任何你想要的,只需简单的修改。)

#  web page scraper that downloads jpgs (or easily PDFs, txt files, csvs, etc.)   AKE  (1/2009)

require 'net/http'

# take instructions from the command line

my_domain_name = ARGV[0]  # JUST the domain name
my_path = ARGV[1]         # the path separated on either end by /
my_filename = ARGV[2]     # the html filename

puts "Scraping #{my_domain_name + my_path + my_filename}"

response = "empty"  

Net::HTTP.start( my_domain_name ) do |http|
     response = http.get( my_path + my_filename ).body
 end

# writing scraped html into text file, for reference / debugging
open("scraped.txt", "wb") {|s|
  s.write(response)
}

 # parse the response  to make list of jpg files
 files = response.scan(/\w+.jpg/)

 # download each jpg
files.each do |file|
   image_filename = file
   puts "Writing #{image_filename}..."
   Net::HTTP.start(my_domain_name) do |http|
   jpg = http.get(file)
   open(image_filename, "wb") {|p|
     p.write(jpg.body)
   }
   end
   puts "Done!"
end
于 2012-08-25T20:25:24.213 回答
0

如果您实际上也想下载所有图像:

for i in `lynx -image_links -dump http://www.google.com | grep 'jpg\|gif' \
| grep http | awk '{print $2}'`; do wget $i; done
于 2013-10-28T17:59:07.823 回答