【问题标题】:Perl: How to get the link target of a soundcloud feed mp3Perl:如何获取 soundcloud feed mp3 的链接目标
【发布时间】:2023-03-05 04:22:01
【问题描述】:

我想在 perl 中的 soundcloud 提要中检索 mp3 的 mp3 链接目标,例如此播客 url 的最终位置:http://feeds.soundcloud.com/stream/176971524-thetalkshow-thetalkshow-100.mp3

如果我尝试流式传输,它会解析为复杂的http://ec-media.soundcloud.com/.... 地址。

有没有可能在 Perl 中获得这个已解析的 ec-media.soundcloud 地址?

我已经尝试了以下代码,但没有成功

use LWP::UserAgent qw();
my $ua = LWP::UserAgent->new;
my $response = $ua->get('http://feeds.soundcloud.com/stream/176971524-thetalkshow-thetalkshow-100.mp3');
print $response->request->uri . "\n";

【问题讨论】:

  • 获取文件本身的 URL 很可能是 soundcloud 动态生成的,即使您能够提取它也可能无法始终如一地工作。

标签: perl soundcloud lwp


【解决方案1】:

您可以使用 LWP::Simple 下载 mp3 文件:

use strict;
use warnings;
use LWP::Simple;
my $url = "http://feeds.soundcloud.com/stream/176971524-thetalkshow-thetalkshow-100.mp3";
getstore($url, "my-file-name.mp3");

【讨论】:

  • 谢谢,不过我不想下载,只想得到最终的文件地址
  • 你在哪里寻找那个 URL?
【解决方案2】:

您的意思是获取 301 或 302 重定向地址。只需禁用自动重定向。

use LWP::UserAgent qw();
my $ua = LWP::UserAgent->new;
$ua->max_redirect(0); # disable auto redirect
my $response = $ua->get('http://feeds.soundcloud.com/stream/176971524-thetalkshow-thetalkshow-100.mp3');
print "new location: " . $response->header('Location');

【讨论】:

  • 感谢您解决了我的问题。但是,我必须在第三行将 max_redirect 从 0 更改为 1。如果您在答案中更改此内容,我将立即将其标记为答案。
  • @Andy 我很好奇,第一个重定向到哪里?
  • 有时我们不知道url会重定向多少次,通常你应该写一个循环,直到没有redirect(301,302 with Location header)响应返回,然后得到最终的url。
  • 并且应该提供一个特殊定义的响应处理程序来获取响应状态和标题,而不是读取正文以防止在最后一次读取 url 时下载真实文件。
猜你喜欢
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多