将所有的HTML绝对链接改为相对的

我有一个网站,有一堆的绝对地址,我需要把它(该网站的整个内容)上一级,所以所有的绝对链接需要转换为相对。

我知道关于wget–convert-links ,但在我的情况下不起作用。 我的网站实际上是用wget镜像的,但是当我用时间戳选项重新获取更新时–convert-links无法正常工作。 还有另外一种方法可以解决吗?

此外,网站是巨大的,所以用另一个镜像工具重新下载是非常不可取的。

该网站由Apache 2.0托pipe,但我无法访问服务器configuration。

您可以执行search并replace您网站中的每个文档以获取所需内容。 也许使用正则expression式模式。 在Linux / Unix中, sed和其他命令行工具可能有所帮助。

我不确定你为什么在谈论wget和镜像工具。 你没有权限访问这些文件吗?

这个工具说,它确实是你想要的:

http://www.perlmonks.org/?node_id=56338

将HTML文件中的“绝对”更改为“相对”链接

此实用程序将通过指定的目录进行recursion,parsing所有的.htm和.html文件,并将相对URL的绝对URLreplace为您定义的基础。

您还可以指定要parsing的链接types:img,src,action或其他。 请参阅模块源代码中HTML :: Tagset的%linkElements哈希值,以精确分解支持的标签types。

这个程序是尝试Getopt :: Declare,一个优秀的命令行parsing器的好习惯。 请注意DATA标签下面的参数说明。

免责声明:总是使用-b开关来强制备份,以防万一您有非标准的HTML和HTML :: TreeBuilderparsing器损坏它。

总是欢迎和非常感谢意见和改进build议。

如果链接停止工作,这里是代码:

 #!/usr/bin/perl -w use strict; use Getopt::Declare; use Cwd; use File::Find; use HTML::TreeBuilder; use URI::URL qw(url); use File::Copy; use IO::File; use vars qw($VERSION $BASE_URL $BACKUP $DIRECTORY @WANTED_TYPES); $VERSION = (qw$Revision: 1.1 $)[1]; #Load the definition and grab the command-line parameters my $opts = Getopt::Declare->new( do{ local $/; <DATA> } ); #Cycle through this directory, and all those underneath it find(\&wanted, $DIRECTORY || getcwd); #Parse each HTML file and make a backup #of it using File::Copy::copy. sub wanted { return unless $File::Find::name =~ /html?$/; #Extract Links from the file my $h = HTML::TreeBuilder->new; $h->parse_file($File::Find::name); my $link_elements = $h->extract_links(@WANTED_TYPES); return unless @$link_elements; #Grab each img src and re-write them so they are relative URL's foreach my $link_element (@$link_elements) { my $link = shift @$link_element; #URL value my $element = shift @$link_element; #HTML::Element Object my $url = url($link)->canonical; next unless $url->can('host_port') and $BASE_URL->host_port eq $url->host_port; #Sigh.. The following is because extract_links() doesn't #tell you which attribute $link belongs to, except to say #it is the value of an attribute in $element.. somewhere. #Given the $link, find out which attribute it was for my ($attr) = grep { defined $element->attr($_) and $link eq $element->attr($_) } @{ $HTML::Tagset::linkElements{$element->tag} }; #Re-write the attribute in the HTML::Element Tree #Note: $BASE_URL needs to be quoted here. $element->attr($attr, $url->path("$BASE_URL")); } #Make a backup of the file before over-writing it copy $File::Find::name => $File::Find::name.'.bak' if defined $BACKUP; #Save the updated file my $fh = IO::File->new($File::Find::name, O_RDWR) or die "Could not open $File::Find::name: $!"; $fh->print($h->as_HTML); } __DATA__ #If there is an error here, you need to have one tab #between the <$var> and the option description. -u <base_url> Base URL (http://www.yoursite.com) [required] { $BASE_URL = url($base_url)->canonical } -b Backup changed files { $BACKUP = 1 } -d <directory> Starting Directory to recurse from { $DIRECTORY = $directory } -l <links>... Links to process: img, href, etc [required] { @WANTED_TYPES = @links }