【问题标题】:How to scrape a site using a User-Agent for Ipad?如何使用 Ipad 的用户代理抓取网站?
【发布时间】:2011-06-22 11:56:06
【问题描述】:

如何使用 Ipad 的用户代理抓取网站?

我在下面使用 PHP 中的 curl 来输出源代码,但仍然找不到标签。在使用 Ipad 用户代理的 Ipad 或 Safari 浏览器上,标签会在网站加载时显示。

谢谢!

<?php
    $useragent= "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10')";

    $ch = curl_init ("http://www.cbsnews.com/video/watch/?id=7370279n&tag=mg;mostpopvideo");

    curl_setopt ($ch, CURLOPT_USERAGENT, $useragent); // set user agent
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    echo $output = curl_exec ($ch);

    curl_close($ch);
?>

【问题讨论】:

  • 您的问题不是很清楚,您想从网页中抓取什么?使用 iPad UA 请求时会发生什么?
  • 当您使用 USER_AGENT 参数通过 curl 发出请求时,CURLOPT_USERAGENT 的内容定义了 HTTP 请求的标头。上述代码不会抓取输出,iPad 抓取输出的功能应该在您请求的 URL 上。
  • @james 我想从 iPad UA 中抓取
  • @mahadeb:我明白了,那么“为 iPad 抓取输出的功能应该在您请求的 URL 上”是什么意思?

标签: php ipad curl user-agent scrape


【解决方案1】:

尝试从命令行使用 curl,使用 perl 脚本,例如:

my $ua = "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10";
my $curl = "curl -A '$ua'";
my $server = "http://www.cbsnews.com";
my $startpage = "$server/video/watch/?id=7370279n&tag=mg;mostpopvideo";
my $path = "/path/to/download/to";
open(f, "$curl -L $startpage |") or die "Cannot open website: $!";
while (<f>)
{
    if (/<a\s+[^>]*href=\"$server\/([^\"\/])*\"/)
    {
        my $file = $2;
        system("$curl -e $startpage $server/$file > $path/$file");
        next;
    }

    if (/<a\s+[^>]*href=\"$server\/([^\"]+)\/([^\"\/])*\"/)
    {
        my $folder = $1;
        my $file = "$folder/$2";
        system("mkdir -p $path/$folder");
        system("$curl -e $startpage $server/$file > $path/$file");
        next;
    }
}
close(f);

【讨论】:

  • 嗨 Nicholas,我尝试了上面的代码,它给了我这个错误消息“curl:没有指定 URL!curl:尝试 'curl --help' 或 'curl --manual' 以获取更多信息”我将代码保存在 .pl 文件中,然后在命令行中加载。
猜你喜欢
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多