php - 用file_get_contents为什么不能抓取的问题?
高洛峰
高洛峰 2017-04-11 10:11:57
[PHP讨论组]

http://www.mayishebao.com/shebao 用file_get_contents为什么不能抓取

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(6)
ringa_lee

php.ini中allow_url_fopen是否开启

PHP中文网

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:

<?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>";

?>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号