我从这个网站上拉任天堂DS价格使用lynx -dump。
例如,假设我要从游戏Yoshi Touch的网页中拉出Go:
/usr/bin/lynx -dump -width=150 http://videogames.pricecharting.com/game/nintendo-ds/Yoshi-Touch-and-Go一切运行正常,我可以使用正则表达式轻松拉动价格。当URL包含一个撇号(')或一个与号(&)时,问题就出现了,因为这会导致错误。因此,假设我尝试查找游戏Yoshi的海岛DS的页面,我将使用下面这行代码:
/usr/bin/lynx -dump -width=150 http://videogames.pricecharting.com/game/nintendo-ds/Yoshi's-Island-DS这会给我一些小错误:
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file下面是我用来调用-dump的代码,其中$fullURL是包含"http://videogames.pricecharting.com/game/nintendo-ds/Yoshi's-Island-DS"“的字符串。
$command = "/usr/bin/lynx -dump -width=150 $fullURL";
@pageFile = `$command`;谁能帮我找到一个解决方案,将$fullURL字符串转换成网址兼容的字符串?
发布于 2012-04-25 03:27:59
您需要转义URL中的',然后才能将其传递给shell。Perl提供了quotemeta函数来执行大多数shell所需的转义。
my $quoted_URL = quotemeta($fullURL);
$command = "/usr/bin/lynx -dump -width=150 $quoted_URL";
...您还可以在字符串中使用\Q和\E转义,以获得相同的结果。
$command = "/usr/bin/lynx -dump -width=150 \Q$fullURL\E";
...发布于 2012-04-25 05:52:10
解决这个问题的正确方法是通过使用system/pipe open (替代qx/backtick运算符)的列表形式来避免外壳,请参阅Perl equivalent of PHP's escapeshellarg。
use autodie qw(:all);
open my $lynx, '-|', qw(/usr/bin/lynx -dump -width=150), $fullURL;
my @pageFile = <$lynx>;
close $lynx;在极少数情况下,这是不切实际的,通过String::ShellQuote和Win32::ShellQuote提供了适当的shell引用。
https://stackoverflow.com/questions/10304412
复制相似问题