如何判断网页是否存在? [英] How to tell if a webpage exists?
问题描述
只是为了好玩,我正在编写一个Perl程序来检查给定的网站是否存在.就我而言,如果我可以进入浏览器,打入URL并获得有意义的网页(意味着没有错误或无法打开页面"消息),则存在一个网站.这样做的最佳方法是什么?最终,我希望能够为我的程序提供数百个网址的列表.
Just for fun, I'm writing a Perl program to check if a given website exists. For my purposes, a website exists if I can go into my browser, punch in the url and get a meaningful webpage (meaning not an error or "failed to open page" message). What would be the best way to go about doing this? Eventually I would like to be able to give my program a list of hundreds of urls.
我正在考虑仅对列表中的每个URL执行ping操作,以查看它们是否存在;但是,我对联网并不是很了解,所以这是最好的方法吗?
I'm thinking about just pinging each of the urls on my list to see if they exist; however, I don't really know too much about networking so is this the best way to do it?
推荐答案
在Perl(LWP)中使用WWW库:
Using Library for WWW in Perl (LWP):
#!/usr/bin/perl
use LWP::Simple;
my $url = 'http://www.mytestsite.com/';
if (head($url)) {
print "Page exists\n";
} else {
print "Page does not exist\n";;
}
这篇关于如何判断网页是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!