如何从其他站点获取HTML代码源

时间:2011-11-22 12:22:29

标签: javascript php jquery html html-parsing

如何获取要启用Cookie的其他网站的HTML代码? 我只需要解析此页面www.fx-trend.com/pamm/rating/

我正在使用javascript jquery(jQMobile),有时使用PHP。(我更喜欢使用js)

这是PHP的示例:

<?php
$url = 'url';
$html = file_get_html($url);
//$html = file_get_contents($url);
echo $html;
?>

以下是js:的示例 How to get data with JavaScript from another server?

OR

$(this).load(url);
alert($(this)); //returns object Object

服务器答案:

  

必须在浏览器中启用Cookie!尝试清除所有cookie,如果   cookie已启用。

欢迎使用代码示例。

1 个答案:

答案 0 :(得分:2)

尝试使用Curl并启用Cookie。下面的代码示例来自this page

<?php
/* STEP 1. let’s create a cookie file */
$ckfile = tempnam ("/tmp", "CURLCOOKIE");

/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ("url");
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$output = curl_exec ($ch);

var_dump($output);

编辑:您可能必须通过更改默认用户代理标头来伪造浏览器。

相关问题