我想保存的网站帖子,因为它可以追溯到几年前,但没有人可以访问数据库后,网站管理员去世。) :(有更好的方法吗?
这就是我到目前为止所拥有的。它是保存主题和什么不是,但不是内容。我想下载每个类别下的帖子,并使用vanilla、myBB、flarum或phpBB (尚未决定)将它们导入SQL。输出:
在./fetch-phpbb.pl第56行使用未初始化的值$posts_in_topic代替(s/)。
未初始化值$year在级联(.)中的使用或字符串位于./fetch-phpbb.pl第66行。
未初始化值$month在级联(.)中的使用或字符串位于./fetch-phpbb.pl第66行。
未初始化值$day在级联(.)中的使用或字符串位于./fetch-phpbb.pl第66行。
打印的宽字符在. ./fetch-phpbb.pl第71行。
未初始化值$posts_in_topic在级联(.)中的使用或
字符串位于./fetch-phpbb.pl第73行。
保存的站点名-主题-00001-页-000--
General_discussion_Expecting_Rain.html (员额总数)
未初始化值$posts_in_topic在数值gt (>)中的使用
./fetch phpbb.pl第75行。
未初始化值$posts_in_topic的使用替换(s/)
./fetch-phpbb.pl第56行。
未初始化值$year在级联(.)中的使用或字符串在
./fetch-phpbb.pl第66行。
未初始化值$month在级联(.)中的使用或字符串在
./fetch-phpbb.pl第66行。
未初始化值$day在级联(.)中的使用或字符串在
./fetch-phpbb.pl第66行。
打印的宽字符在. ./fetch-phpbb.pl第71行。
#!/usr/bin/perl
use strict;
use warnings;
require LWP::UserAgent;
use POSIX qw/ceil/;
my $ua = LWP::UserAgent->new(agent => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20100101 Firefox/10.0.2');
$ua->timeout(10);
my $sid = 'efba981e5f1d54639b2bb67d8adf2a11';
$ua->default_header('Host' => 'expectingrain.com');
$ua->default_header('Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
$ua->default_header('Accept-Language' => 'en-us,en;q=0.5');
$ua->default_header('Accept-Encoding' => 'gzip, deflate');
$ua->default_header('DNT' => '1');
$ua->default_header('Connection' => 'keep-alive');
$ua->default_header('Cache-Control' => 'max-age=0');
#$ua->default_header();
my $max_topic_id = 20000;
my $posts_per_topic_page = 30;
my $topics_per_forum_page = 50;
my $output_file_name_base = 'sitename-';
my $base_url = 'https://expectingrain.com/discussions/viewforum.php?f=6';
my %status = ();
for (my $i = 1; $i <= $max_topic_id; $i++) {
my $posts_in_topic = 0;
my $url = $base_url . '?sid=' .$sid . '&t='.$i;
my $response = $ua->get($url );
if ($response->is_success) {
my $page_content = $response->decoded_content;
# [ 621 posts ]
if ($page_content =~ /topic does not exist/) {
print STDERR "$url: Topic $i does not exist\n";
$status{'invalid topic'}++;
} elsif ($page_content =~ /not authorised/) {
print STDERR "$url: Not authorized\n";
$status{'unauthorized'}++;
} elsif ($page_content =~ /requires you to be registered and logged in/) {
print STDERR "$url: Login prompt.\n";
$status{'login message'}++;
} elsif ($page_content =~ /forum you selected does not exist/) {
print STDERR "$url: Forum does not exist\n";
$status{'forum dne'}++;
} else {
$posts_in_topic = ($page_content =~ / ([\d,]+) post(s?) /)[0];
$posts_in_topic =~ s/,//;
$status{'valid post'}++;
# print "Topic #$i contains $posts_in_topic posts\n";
my $title = ($page_content =~ /<title>(.+)<\/title>/)[0];
$title =~ s/[^\w\s]+//g;
$title =~ s/\s+/_/g;
# <td class="postbottom" align="center">Sat Nov 01, 2008 9:00 pm</td>
my ($crap, $wd, $month, $day, $year) = ($page_content =~ />((\w{3}) (\w{3}) (\d{2}), (\d{4})) \d+:\d{2} ..<\/td/);
my $date_str = "$year-$month-$day";
my $output_filename = sprintf("$output_file_name_base"."topic-%05d-page-000-$date_str-$title.html",$i);
open(FILE, ">$output_filename") or die $!;
print FILE $page_content;
close(FILE);
print "Saved $output_filename ($posts_in_topic total posts)\n";
if ($posts_in_topic > $posts_per_topic_page) {
my $total_pages = ceil($posts_in_topic / $posts_per_topic_page);
for (my $j = 1; $j < $total_pages; $j++) {
my $start = $j * $posts_per_topic_page;
my $page_url = $url . '&start='.$start;
my $page_filename = sprintf("$output_file_name_base".'topic-%05d-page-%03d.html',$i, $j);
my $page_response = $ua->get($page_url, ':content_file' => $page_filename);
if ($page_response->is_success) {
print "Saved Content File $page_filename\n";
} else {
print STDERR "Error fetching $page_url: {$response->status_line}\n";
}
}
}
# print $page_content;
}
} else {
print STDERR "Error fetching $url: {$response->status_line}\n";
$status{'error:'.$response->status_line}++;
}
}发布于 2021-12-27 14:20:52
因此,在第一个请求中,您的程序将获得以下URL:
这将返回200个响应代码,并且内容不包含代码查找的字符串,因此您将在else代码中的if ($page_content =~ /.../)块中结束。从以下几行开始:
$posts_in_topic = ($page_content =~ / ([\d,]+) post(s?) /)[0];
$posts_in_topic =~ s/,//;它搜索页面内容中的字符串,该字符串由多个数字和逗号组成,后面跟着空格、单词"post“和可选的"s”。然后从找到的字符串中移除第一个逗号。这是第二行(移除逗号的那一行),给出的错误是:
使用未初始化的值$posts_in_topic替换./fetch-phpbb.pl第56行中的(s/)。
但是,如果您查看页面,您会发现没有一个字符串由一个数字(包括逗号)后面的"post“或”post“组成。然而,页面顶部有一个字符串,上面写着"27559主题“。
接下来的错误如下:
将未初始化的值$year用于级联(.)或字符串位于./fetch-phpbb.pl第66行。
未初始化值$month在级联(.)中的使用或字符串位于./fetch-phpbb.pl第66行。
未初始化值$day在级联(.)中的使用或字符串位于./fetch-phpbb.pl第66行。
如果您查看代码,您将看到类似的代码:
my ($crap, $wd, $month, $day, $year) = ($page_content =~ />((\w{3}) (\w{3}) (\d{2}), (\d{4})) \d+:\d{2} ..<\/td/);
my $date_str = "$year-$month-$day";同样,我们试图匹配数据并将其存储在变量中。我们正在寻找三个字母数字(\w{3})、三个字母数字(再次是\w{3})、两个数字(\d{2})和四个数字(\d{4}),并将它们存储在周末的变量$wd、$month、$day和$year中。所有这些让我觉得我们在寻找类似于“2021年12月27日”的日期。再看一遍,页面上没有那个格式的日期。所有的日期都是“2021年12月27日星期一”。
我不知道这些代码是从哪里来的,但在我看来,它是为不同版本的phpBB编写的--它以不同于代码所期望的方式格式化页面上的数据。
您可能可以通过更改代码来解决这些问题。
$posts_in_topic = ($page_content =~ / ([\d,]+) post(s?) /)[0];应成为:
$posts_in_topic = ($page_content =~ /([\d,]+) topic(s?)/)[0];和
my ($crap, $wd, $month, $day, $year) = ($page_content =~ />((\w{3}) (\w{3}) (\d{2}), (\d{4})) \d+:\d{2} ..<\/td/);应该变成这样:
my ($wd, $month, $day, $year) = ($page_content =~ />(\w{3}) (\w{3,9}) (\d{2})\w\w, (\d{4}) \d+:\d{2} ..<\/td/);(实际上,最后一个不起作用,因为页面上的HTML也不同--我没有时间做进一步的调查。)
所以,总的来说,这个程序不是为你的网站制作的。您可能会对此进行黑客攻击,或者您可能会找到一个可用于您版本的phpBB。
但我真的认为最好的选择是与网络托管公司取得联系,解释情况,看看他们如何帮助你。老实说,根据我的经验,大多数挪威人的英语比大多数英国人讲得更好:-)
https://stackoverflow.com/questions/70491152
复制相似问题