[英]How can I handle HTTP redirects in my Perl application?
I use this Perl subroutine to get a line from a webpage, which I then split to get the information I require to proceed. 我使用此Perl子例程从网页获取一行,然后将其拆分以获取所需的信息。 This has worked for a dozen years.
这已经工作了十二年。
sub capture_line {
my $page_to_get = $_[0];
my $host_to_get_text = $_[1];
my $port = 80;
my $buf = &HTTPGet($page_to_get, $host_to_get_text, $port);
my $image_capture_text;
my @lines = split(/\n/,$buf);
# print "$lines[1]\n";
# print "$page_to_get, $host_to_get_text\n";
# print "$buf\n";
foreach (@lines) {
if (/$text_to_find/i) {
$image_capture_text = $_;
print "in_loop";
last;
}
}
return $image_capture_text;
}
Unforntuately, $page_to_get
is now always a 301 redirect and $buf
, when printed, gives me a 301 redirection page, which obviously does not contain the sought after text. 不幸的是,
$page_to_get
现在始终是301重定向,而$buf
在打印时为我提供了301重定向页面,该页面显然不包含所追求的文本。 Is there a $in
value pair (for example) that I can use with HTTPGet
to hop me over the redirection so that I get the page that I see when I type http://$host_to_get_text$page_to_get
into my browser? 是否存在可以与
HTTPGet
一起使用的$in
值对(例如),使我跳过重定向,以便获得在浏览器中输入http://$host_to_get_text$page_to_get
时看到的页面? Or is there a better way to accomplish the same thing (knowlege of an ever changing filename in the source of a viewed webpage)? 还是有更好的方法来完成相同的事情(在浏览的网页源中了解不断变化的文件名)?
Thank you for your time. 感谢您的时间。 Greg Marsh
格雷格·马什
Where is the HTTPGet
function coming from? HTTPGet
函数从哪里来?
If you were to use LWP ( http://search.cpan.org/dist/libwww-perl/ ) to do the HTTP fetching, that will automatically follow redirects (you can specify how many times you want it to follow a redirect before giving up). 如果您要使用LWP( http://search.cpan.org/dist/libwww-perl/ )进行HTTP提取,则会自动执行重定向操作(您可以指定希望其执行重定向操作的次数之前,放弃)。
eg: 例如:
use LWP::Simple qw()
my ($page_to_get, $host_to_get_text) = @_;
my $url = "http://$host_to_get_text$page_to_get";
my $buf = LWP::Simple::get($url);
my $image_capture_text;
my @lines = split(/\n/,$buf);
# ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.