我想登录到社区服务器的论坛部分(例如http://forums.timesnapper.com/login.aspx?ReturnUrl=/forums/default.aspx),然后下载特定页面并执行正则表达式(查看是否有任何帖子等待审核)。如果有,我想发一封电子邮件。
我想从 Linux 服务器执行此操作。
目前我知道如何下载一个页面(例如使用 wget),但在登录时遇到问题。有什么好主意吗?
我想登录到社区服务器的论坛部分(例如http://forums.timesnapper.com/login.aspx?ReturnUrl=/forums/default.aspx),然后下载特定页面并执行正则表达式(查看是否有任何帖子等待审核)。如果有,我想发一封电子邮件。
我想从 Linux 服务器执行此操作。
目前我知道如何下载一个页面(例如使用 wget),但在登录时遇到问题。有什么好主意吗?
您可能对 Selenium 有更好的运气或查看此问题以获得更多建议:
查看登录页面的来源,它似乎是一个 asp.net 应用程序,因此您可能需要做几件事来实现这一点 -
管理表单隐藏的 __viewstate 字段,并在您提交登录详细信息时将其发回。
一旦你过去了,我猜你可以只使用绝对 URL 来引用有问题的特定页面,但你需要处理 ASP.NET Forms 身份验证 cookie 并将其作为 GET 请求的一部分发送。
就个人而言,我会用 Perl 编写它,使用WWW::Mechanize,并执行以下操作:
my $login_url = 'login url here';
my $username = 'username';
my $password = 'password';
my $mech = new WWW::Mechanize;
$mech->get($login_url)
or die "Failed to fetch login page";
$mech->set_visible($username, $password)
or die "Failed to find fields to complete";
$mech->submit
or die "Failed to submit form";
if ($mech->content() =~ /posts awaiting moderation/i) {
# Do something here
}
我不知道上述方法是否可行,因为我没有社区服务器(无论是什么)的登录详细信息来测试它,但它应该为您提供一些您可以轻松工作的东西,并显示出强大的功能万维网::机械化。
您可以使用 wget 完成所有操作。您需要使用 POST 提交表单并需要存储 cookie。wget 手册页中的相关内容:
--post-data=string
--post-file=file
Use POST as the method for all HTTP requests and send the specified data in the request body.
"--post-data" sends string as data, whereas "--post-file" sends the contents of file. Other than
that, they work in exactly the same way.
This example shows how to log to a server using POST and then proceed to download the desired pages,
presumably only accessible to authorized users:
# Log in to the server. This can be done only once.
wget --save-cookies cookies.txt \
--post-data 'user=foo&password=bar' \
http://server.com/auth.php
# Now grab the page or pages we care about.
wget --load-cookies cookies.txt \
-p http://server.com/interesting/article.php