PHP:如何根据 Javascript 抓取网站内容 [英] PHP: How to scrape content of the website based on Javascript

查看:40
本文介绍了PHP:如何根据 Javascript 抓取网站内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get content of this website using PHP simplehtmldom library.

http://www.immigration.govt.nz/移民/流/工作/workingholiday/czechwhs.htm"

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

It is not working, so i tried using 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 代码和内容:

But always get only respose with some JS code and content:

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

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

Is any possibility to solve this using PHP? I must use PHP in this case so i need to simulate JS based browser.

非常感谢您的建议.

推荐答案

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

I must use PHP in this case so i need to simulate JS based browser.

我推荐你两种方式:

  1. 利用v8js php插件在抓取时处理站点的js.请参阅此处使用示例.
  2. 使用 Selenium 模拟基于 JS 的浏览器、iMacros 或 webRobots.io Chrome 扩展.但在这种情况下,您没有使用 PHP 脚本.
  1. Leverage v8js php plugin to deal with site's js when scraping. See here an usage example.
  2. Simulate JS based browser thru using Selenium, iMacros or webRobots.io Chrome ext. But in this case you are off the PHP scripting.

这篇关于PHP:如何根据 Javascript 抓取网站内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆