http://www.mayishebao.com/shebao 用file_get_contents为什么不能抓取
你想要抓取什么呢?不防转换一下数据类型试试?比如我想file_get_contents()使用抓取http://www.mayishebao.com/shebao这个页面的title:
title.php:
<?php
echo "<html>";
echo "<body>";
echo "<head><title>抓取title</title></head>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
echo "<center><h1>抓取title<h1></center></br>";
echo "<form action=\"title2.php\" method=\"post\">";
echo "请输入需要抓取title的链接(比如:<a href=http://www.mayishebao.com/shebao target=\"_blank\" style=\"color:red;\">http://www.app-echo.com/sound/884556</a>):<input type=\"text\" name=\"echo\" value=\"\"> ";
echo "<input type=\"submit\" value=\"抓取title\" />";
echo "</form>";
echo"</body>";
echo "</html>";
?>
title2.php:
<?php
echo "<html>";
echo "<body>";
echo "<head><title>抓取title</title></head>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
$html = file_get_contents(implode($_POST));
$from="<title>";
$end="</title>";
function cut($file,$from,$end){
$message=explode($from,$file);
$message=explode($end,$message[1]);
return $message[0];
}
$title=cut($html, $from, $end);
$title2=str_replace('\\','',$title);
echo $title2;
echo"</body>";
echo "</html>";
?>
获取本地内容可用file_get_contents
, 获取外部内容推荐用curl
,推荐用curl
参看之前的回答php file_get_contents 不能获得内容
ps:提问前最好搜索下相关问题
有些是有防爬虫策略的啊,需要设置header信息什么的,还是像楼上说的用curl吧
echo file_get_contents(); 不要赋值直接echo出来,如果有数组,就是没有转类型导致的!
$html = file_get_contents($url);
$html = iconv("gb2312", "utf-8//IGNORE",$html);
echo $html;
php.ini中allow_url_fopen是否开启
file_get_contents理论上不可能不能抓取啊,又不是https,你抓取后,返回了什么,报错了吗?我们连你的上下文都不清楚怎么帮你分析问题啊