[英]How to parse contents of PHP file_get_contents as HTML in jQuery?
[英]file_get_contents and jQuery pageless
我正在使用php file_get_contents函數從pinterest的源跟蹤頁面檢索HTML,該頁面顯示了源自特定域的所有引腳。 例如: http : //pinterest.com/source/google.com/
但是,pinterest似乎正在使用jQuery無頁面功能,這阻止了所有內容的加載。
有沒有一種方法可以強制file_get_contents函數觸發無頁面函數,以便返回整個結果集?
file_get_contents(..)
只是為您提供瀏覽器中的Page源。 它不能提供通過javascript加載的內容。 在您的情況下,最好的方法是查找正在進行的AJAX調用(在頁面源中)。 或者,您可以打開瀏覽器的實用程序來監視頁面活動。 (在Chrome上,您可以使用ctrl + shift + J來獲得它)
一旦獲得了向其發出請求的URL,就可以在file_get_contents(..)
直接使用它們來獲取相關數據。
嘗試了file_get_contents,但是由於某種原因並沒有給我太多幫助,但是cURL似乎對我來說很好。
您將需要在服務器上安裝cURL,以及PHP的libCURL擴展,但是您可以嘗試執行以下操作,然后查看得到的結果:
<?php
$cl = curl_init();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3";
$header[] = "Accept-Language: nb-NO,nb;q=0.8,no;q=0.6,nn;q=0.4,en-US;q=0.2,en;q=0.2";
$header[] = "Pragma: ";
curl_setopt($cl, CURLOPT_FAILONERROR,true);
curl_setopt($cl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7');
curl_setopt($cl, CURLOPT_HTTPHEADER, $header);
curl_setopt($cl, CURLOPT_REFERER, 'http://www.google.com');
curl_setopt($cl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($cl, CURLOPT_AUTOREFERER, false);
curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cl, CURLOPT_CONNECTTIMEOUT, 2);
$url = 'http://pinterest.com/source/google.com/';
curl_setopt($cl, CURLOPT_URL, $url);
$output = curl_exec($cl);
curl_close($cl);
?>
<!DOCTYPE html>
<head>
<title>get pinterest</title>
</head>
<body>
<xmp>
<?php echo $output; ?>
</xmp>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.