【问题标题】:PHP: How to scrape content of the website based on JavascriptPHP:如何基于 Javascript 抓取网站内容
【发布时间】:2015-03-05 17:51:35
【问题描述】:

我正在尝试使用 PHP simplehtmldom 库获取该网站的内容。

http://www.immigration.govt.nz/migrant/stream/work/workingholiday/czechwhs.htm"

它不起作用,所以我尝试使用 CURL:

function curl_get_file_contents($URL)
{
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_URL, $URL);
    $contents = curl_exec($c);
    curl_close($c);

    if ($contents) return $contents;
    else return FALSE;
}

但总是只响应一些 JS 代码和内容:

<noscript>Please enable JavaScript to view the page content.</noscript>

有没有可能使用 PHP 来解决这个问题?在这种情况下我必须使用 PHP,所以我需要模拟基于 JS 的浏览器。

非常感谢您的任何建议。

【问题讨论】:

    标签: javascript php curl web-scraping noscript


    【解决方案1】:

    在这种情况下我必须使用 PHP,所以我需要模拟基于 JS 的浏览器。

    我推荐你两种方法:

    1. 抓取时利用v8js php plugin处理站点的js。请参阅 here 的用法示例。
    2. 通过使用Selenium、iMacros 或 webRobots.io Chrome ext 模拟基于 JS 的浏览器。但在这种情况下,您将脱离 PHP 脚本。

    【讨论】:

    • @redrom,感谢您检查我的答案,您能分享一下这两个选项中的哪些对您有帮助吗?你是如何应用它的?我问你这个,因为我做网络抓取研究,然后将结果发布到 scraping.pro 博客。任何反馈都将是可观的!
    猜你喜欢
    • 2019-04-23
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多