简体   繁体   English

如何编写 Perl 脚本以使用 curl 处理 URL?

[英]How do I write a Perl script to use curl to process a URL?

I have a very simple task.我有一个非常简单的任务。 I have a crontab that will run a script every hour.我有一个 crontab,它将每小时运行一个脚本。 The script is meant to simply process a URL.该脚本旨在简单地处理 URL。

This is what I have.这就是我所拥有的。 It doesn't work.它不起作用。 I get a syntax error.我收到语法错误。

#!/usr/bin/perl
curl http://domain.com/page.html;

I haven't worked in Perl in years and wasn't very adept when I did.我已经很多年没有在 Perl 工作了,而且我做的时候也不是很熟练。

SOLUTION解决方案

Thanks, Alex for pointing me to the correct path!谢谢,亚历克斯为我指出了正确的道路!

crontab定时任务表

*/30 * * * * curl http://domain.com/path.html

You can either use curl via backticks您可以通过反引号使用curl

my $curl=`curl http://whatever`

or you can use WWW::Curl .或者你可以使用WWW::Curl

To call any shell command from Perl, you can use system :要从 Perl 调用任何 shell 命令,您可以使用system

system "curl http://domain.com/page.html";

Just enclose the shell command in quotes.只需将 shell 命令括在引号中即可。

If shying away from executing command line tools from with Perl, the library equivalent is如果避免使用 Perl 执行命令行工具,则库的等效项是

use WWW::Curl::Simple;
my $curl = WWW::Curl::Simple->new();
my $res = $curl->get("https://stackoverflow.com");

The content you then access as in然后您访问的内容如下

print $res->content;

You may want to read up on https://metacpan.org/pod/WWW::Curl::Simple and the get method returns a https://metacpan.org/pod/HTTP::Response .您可能想阅读https://metacpan.org/pod/WWW::Curl::Simple并且 get 方法返回一个https://metacpan.org/pod/HTTP::Response

Try this if you need to pass multiple parameters.如果您需要传递多个参数,请尝试此操作。

my $cc = 'curl -v -X GET https://base-template.squarespace.com/blog/\?field1=value1\&format=json-pretty\&field3=value3';
print `$cc`;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM