【问题标题】:file_get_content get the wrong webfile_get_content 获取错误的网络
【发布时间】:2013-08-21 15:47:25
【问题描述】:

我正在学习使用 PHP-file_get_contents 爬取网站内容,但出现了问题。我想要的网络是“http://www.jandan.net”。

但是使用file_get_content(),我从“http://i.jandan.net”获取内容(这是电话页面,它们是不同的页面)。 user_agent 也无法使用。

<?php
ini_set("user_agent","Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2) Gecko/20100301 Ubuntu/9.10 (karmic) Firefox/3.6");
$url = 'http://www.jandan.net/';
/*
$opt = array( 'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: Mozilla/5.0\n"
)
);
$context = stream_context_create($opt);
*/
$content = file_get_contents($url);
echo var_dump($content);
?>

【问题讨论】:

  • 两个 URL 对我来说都很好。还是我误解了那些中文 404 页面?
  • 您是否考虑过使用 curl 而不是 file_get_contents?如果出现问题,使用 curl 更改用户代理非常简单。 curl_setopt($ch,CURLOPT_USERAGENT, '')
  • “但使用 file_get_content()” 它是 file_get_contents() ;-) 加上尝试删除 ($url,) 中的逗号,仅此一项就会引发错误。
  • @AmalMurali 您的意思是,您在按原样使用 OP 代码时没有收到解析错误? => ($url,)
  • @Fred-ii-:我做到了,但我只是(盲目地)认为这只是一个打字错误。

标签: php web-crawler


【解决方案1】:

$content = file_get_contents($url,); 中的逗号导致了问题。

----------------------------------- --------------------------^

来自original posted code ---^

保留逗号将产生以下错误消息:

解析错误:语法错误,意外的')' in.....(文件夹路径等)

快速说明:使用$url = 'http://i.jandan.net/'; 也可以,显示内容。

试试这个:

<?php
ini_set("user_agent","Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2) Gecko/20100301 Ubuntu/9.10 (karmic) Firefox/3.6");
$url = 'http://www.jandan.net/';

/*
$opt = array( 'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: Mozilla/5.0\n"
)
);
$context = stream_context_create($opt);
*/
$content = file_get_contents($url);
echo var_dump($content);
// echo $content;
?>

【讨论】:

    猜你喜欢
    • 2011-04-26
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 2016-12-30
    • 2023-03-23
    • 1970-01-01
    • 2016-12-20
    • 2020-11-29
    相关资源
    最近更新 更多