扫码关注官方订阅号
http://www.mayishebao.com/shebao 用file_get_contents为什么不能抓取
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
php.ini中allow_url_fopen是否开启
echo file_get_contents(); 不要赋值直接echo出来,如果有数组,就是没有转类型导致的!
$html = file_get_contents($url); $html = iconv("gb2312", "utf-8//IGNORE",$html); echo $html;
获取本地内容可用file_get_contents, 获取外部内容推荐用curl,推荐用curl
file_get_contents
curl
参看之前的回答php file_get_contents 不能获得内容
ps:提问前最好搜索下相关问题
file_get_contents理论上不可能不能抓取啊,又不是https,你抓取后,返回了什么,报错了吗?我们连你的上下文都不清楚怎么帮你分析问题啊
有些是有防爬虫策略的啊,需要设置header信息什么的,还是像楼上说的用curl吧
你想要抓取什么呢?不防转换一下数据类型试试?比如我想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>"; ?>
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
php.ini中allow_url_fopen是否开启
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理论上不可能不能抓取啊,又不是https,你抓取后,返回了什么,报错了吗?我们连你的上下文都不清楚怎么帮你分析问题啊
有些是有防爬虫策略的啊,需要设置header信息什么的,还是像楼上说的用curl吧
你想要抓取什么呢?不防转换一下数据类型试试?比如我想file_get_contents()使用抓取http://www.mayishebao.com/shebao这个页面的title:
title.php:
title2.php: