Incrementing Through URLs and Downloading

≡放荡痞女 提交于 2019-12-12 04:48:58

问题


I would just like a simple browser automation that increments one number in a URL and downloads the information from that place. For example, if the address looks like this:

www.test.com/something/part1_0.jpg

How could I increment the '1' and download the file from each successive web page?

Thanks

P.S. I'm using OS X 10.9


回答1:


Here's a ruby solution using open-uri:

require 'open-uri'

(1..100).each do |num|                                          
  File.open("part#{num}_0.jpg", 'wb') do |f|
    f.write open("www.test.com/something/part#{num}_0.jpg").read 
  end
end

This snippet A) creates a range of numbers; B) iterates over the range of numbers; C) opens an image file in binary mode and interpolates the current number into the file name; and D) reads the image from the URL and writes it.

But the easiest way would probably be to use curl from your command line:

curl -O www.test.com/something/part[1-100]_0.jpg

Depending on the number of webpages that you need to access, modify the numbers in brackets accordingly.



来源:https://stackoverflow.com/questions/28787295/incrementing-through-urls-and-downloading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!