http://www.mayishebao.com/shebao 用file_get_contents为什么不能抓取
http://www.mayishebao.com/shebao 用file_get_contents为什么不能抓取
echo file_get_contents(); 不要赋值直接echo出来,如果有数组,就是没有转类型导致的!
$html = file_get_contents($url);
$html = iconv("gb2312", "utf-8//IGNORE",$html);
echo $html;
获取本地内容可用file_get_contents
, 获取外部内容推荐用curl
,推荐用curl
参看之前的回答php file_get_contents 不能获得内容
ps:提问前最好搜索下相关问题
你想要抓取什么呢?不防转换一下数据类型试试?比如我想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>";
?>
2 回答1.5k 阅读✓ 已解决
1 回答1.3k 阅读✓ 已解决
2 回答954 阅读✓ 已解决
1 回答1.1k 阅读✓ 已解决
2 回答1k 阅读
1 回答977 阅读
1 回答902 阅读
php.ini中allow_url_fopen是否开启