我通常使用WGET从一些网页上下载一两张图片,我会在命令提示符下这样做:wget 'webpage-url' -P 'directory to where I wanna save it'。现在,我如何在Perl和Python中实现自动化呢?这就是什么命令可以让我模拟在命令提示符下输入命令?在Python中,有很多类似的模块,比如subprocess、os等等,这让我很困惑。
发布于 2012-08-04 18:45:23
import subprocess
subprocess.call(["wget", "www.example.com", "-P", "/dir/to/save"])如果您想读取URL并处理响应:
import urllib2
response = urllib2.urlopen('http://example.com/')
html = response.read()如何从可以阅读here的html中提取图像
发布于 2012-08-04 19:15:47
在Perl中,最简单的方法是使用LWP::Simple。
use LWP::Simple qw(getstore);
getstore('www.example.com', '/path/to/saved/file.ext');发布于 2012-08-04 20:17:41
在Perl中,也可以使用qx(yourcommandhere)。这是程序的外部调用。
因此,在您的示例中:qx(wget 'webpage-url' -P '/home/myWebPages/')。这对你来说已经足够了。
但是,正如s0me0ne所说,使用LWP::Simple更好。
如果文件中有urls列表,则可以使用以下代码:
my $fh; # filehandler
open $fh, "<", "fileWithUrls.txt" or die "can't find file with urls!";
my @urls = <$fh>; # read all urls, one in each raw of file
my $wget = '/path/to/wget.exe';
for my $url(@urls) {
qx($wget $url '/home/myWebPages/');
}https://stackoverflow.com/questions/11807932
复制相似问题