PHP:如何抓取基于Javascript的网站内容 [英] PHP: How to scrape content of the website based on Javascript
本文介绍了PHP:如何抓取基于Javascript的网站内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用PHP simplehtmldom库来获取本网站的内容。
I'm trying to get content of this website using PHP simplehtmldom library.
http://www.immigration.govt.nz/migrant/stream/work/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代码和内容进行respose:
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的浏览器。 p>
I must use PHP in this case so i need to simulate JS based browser.
我会推荐两种方式:
- 在抓取时利用 v8js php插件处理网站的js。请参见此处使用示例。
- 使用模拟基于JS的浏览器 Selenium ,iMacros或者webRobots.io Chrome ext。但在这种情况下,你离开PHP脚本。
- Leverage v8js php plugin to deal with site's js when scraping. See here an usage example.
- 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屋!
查看全文