简体   繁体   English

加载js后获取网页内容

[英]Get web page content after load js

I send request to website for get content by file_get_contents eg 我将请求发送到网站以通过file_get_contents获取内容,例如

$html=file_get_contents('http://....');
var_dump(HTML::encode($html));

but html body tag fill by js so I cant get body. 但是html正文标记由js填充,所以我无法获得正文。 body is like this 身体是这样的

 <body> </body> 

How can get body by php php如何获取身体

You can use tools specifically designed for this purpose. 您可以使用专门为此目的设计的工具。

A popular solution is Symfony's Panther library . 流行的解决方案是Symfony的Panther库

Given the page you are trying to get content for is hosted at http://example.com , and an element with the id "myElement" is added to the page using javascript (indicating the javascript we are dependent on has finished executing), we could run the following code: 给定您要获取其内容的页面托管在http://example.com上 ,并且使用javascript将id为“ myElement”的元素添加到该页面(表明我们所依赖的javascript已经完成执行),我们可以运行以下代码:

$client = \Symfony\Component\Panther\Client::createChromeClient();
$crawler = $client->request('GET', 'http://example.com');
$client->waitFor('#myElement');
var_dump($crawler->html());

if the target website contents are being populated by the script then you cannot access it via above method as there is no area provided to execute the script to populate the body when you do a PHP call like above. 如果目标网站的内容是由脚本填充的,则您无法通过上述方法访问它,因为当您进行上述PHP调用时,没有提供执行脚本来填充正文的区域。 alternatively, you may use ajax to get the target website content which also will have restrictions based on origin/request which only possible if you have access to the target website or you can use an iframe and I don't know which is suitable for what you really need to accomplish anyway? 或者,您可以使用Ajax获取目标网站的内容,该内容也将基于源/请求进行限制,只有在您可以访问目标网站或可以使用iframe且我不知道哪种内容适合时才可能有限制你真的需要完成吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM