跳转到主内容
趣航编程网 - 趣学编程,启航技术之路!

PHP实现抓取页面与代码解析_PHP教程

在做一些天气预报或者RSS订阅的程序时,往往需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过http请求访问url地址, 然后得到html源代码或者xml数据,得到数据我们不能直接输出,往往需要对内容进行提取,然后再进行格式化,以更加友好的方式显现出来。

一、 PHP抓取页面的主要方法:

1. file()函数

2. file_get_contents()函数

3. fopen()->fread()->fclose()模式

4.curl方式

5. fsockopen()函数 socket模式6. 使用插件(如:http://sourceforge.net/projects/snoopy/)

二、PHP解析html或xml代码主要方式:

1. 正则表达式    2. PHP DOMDocument对象  3. 插件(如:)如果你对以上内容已经很了解,以下内容可以飘过……

PHP抓取页面

1. file()函数

php $url='http://t.qq.com'; $lines_array=file($url); $lines_string=implode('',$lines_array); echo htmlspecialchars($lines_string); ?

2. file_get_contents()函数

使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置 allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件。

php $url='http://t.qq.com'; $lines_string=file_get_contents($url); echo htmlspecialchars($lines_string); ?

3. fopen()->fread()->fclose()模式

php $url='http://t.qq.com'; $handle=fopen($url,"rb"); $lines_string=""; do{ $data=fread($handle,1024); if(strlen($data)==0){break;} $lines_string.=$data; }while(true); fclose($handle); echo htmlspecialchars($lines_string); ?

4. curl方式

使用curl必须空间开启curl。方法:windows下修改php.ini,将extension=php_curl.dll前面的分号去掉,而且需 要拷贝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安装curl扩展。

php $url='http://t.qq.com'; $ch=curl_init(); $timeout=5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $lines_string=curl_exec($ch); curl_close($ch); echo htmlspecialchars($lines_string); ?5. fsockopen()函数 socket模式socket模式能否正确执行,也跟服务器的设置有关系,具体可以通过phpinfo查看服务器开启了哪些通信协议,比如我的本地php socket没开启http,只能使用udp测试一下了。

php $fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr); if (!$fp) { echo "ERROR: $errno - $errstr

6. 插件

网上应该有比较多的插件,snoopy插件是在网上搜到的,有兴趣的可以研究一下。

PHP解析xml(html)

1. 正则表达式:

php $url='http://t.qq.com'; $lines_string=file_get_contents($url); eregi('2. PHP DOMDocument()对象如果远程的html或xml存在语法错误,php在解析dom的时候会报错。</p> <p>php $url='http://www.136web.cn'; $html=new DOMDocument(); $html-loadHTMLFile($url); $title=$html->getElementsByTagName('title'); echo $title->item(0)->nodeValue; ?></p> <h3>3. 插件</h3> <p>本文以PHP Simple HTML DOM Parser为例,进行简单介绍,simple_html_dom的语法类似jQuery,它让php操作dom,就像使用jQuery操作dom一样的简单。</p> <p>php $url='http://t.qq.com'; include_once('../simplehtmldom/simple_html_dom.php'); $html=file_get_html($url); $title=$html-find('title'); echo $title[0]->plaintext; ?>当然中国人是富有创造性的,老外往往会在技术上领先,但中国人往往会在使用上更胜一筹,往往做出一些让老外不敢想的功能,比如php的远程抓取与分 析,本来是为数据的整合提供方便。但国人很喜欢这个,于是乎大量的采集站,它们本身不创造任何有价值的内容,就是靠抓取别人的网站内容,并把它据为己有。 在百度里输入“php小”关键词,suggest列表第一个就是“php小偷程序”,然后把同样的关键词放入google,哥只能笑而不语。更多问题欢迎到问答网(shenzhidao.com)提问!</p> <p>摘自 网络世界无中生有http://www.bkjia.com/PHPjc/478450.html www.bkjia.com true http://www.bkjia.com/PHPjc/478450.html TechArticle在做一些天气预报或者RSS订阅的程序时,往往需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过http请求访问url地址,...</p> </div> <!-- 文章导航 --> <nav class="cxy-article-nav" aria-label="文章导航"> <a href="https://www.vqhf.com/article/6549.html" class="cxy-article-nav-link cxy-article-nav-prev"> <span>‹</span> <div> <div class="cxy-article-nav-label">上一篇</div> <div class="cxy-article-nav-title">C++中如何实现持续集成_自动化构建系统</div> </div> </a> <a href="https://www.vqhf.com/article/6555.html" class="cxy-article-nav-link cxy-article-nav-next"> <div> <div class="cxy-article-nav-label">下一篇</div> <div class="cxy-article-nav-title">c# ConcurrentDictionary 内部是如何实现分段锁的</div> </div> <span>›</span> </a> </nav> </div> </article> <!-- 相关文章列表 --> <section class="cxy-content-section" style="margin-top: 20px;"> <h2 class="cxy-widget-title" style="margin-bottom: 20px;">相关文章</h2> <ul class="cxy-article-list"> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6549.html" class="cxy-article-link">C++中如何实现持续集成_自动化构建系统</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6544.html" class="cxy-article-link">PHP怎样操作文件_文件读写与删除操作指南【手册】</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6542.html" class="cxy-article-link">如何用 Golang 实现实时数据处理_Golang 并发管道与消息传递模式</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6536.html" class="cxy-article-link">如何在C++中实现一个轻量级的定时器?(回调函数)</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6535.html" class="cxy-article-link">Golang如何构建可读性强的错误信息_Golang错误文案设计</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6555.html" class="cxy-article-link">c# ConcurrentDictionary 内部是如何实现分段锁的</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6557.html" class="cxy-article-link">php图像处理常用函数与实例_PHP教程</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6562.html" class="cxy-article-link">坛里有朋友搞过阿里巴巴API的授权请求吗解决方法</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6565.html" class="cxy-article-link">php生成N个不重复的随机数</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6568.html" class="cxy-article-link">PHP 框架如何影响应用程序响应时间?</a> <div class="cxy-article-meta">2026-09-14</div> </li> </ul> </section> </div> <!-- 右侧边栏 --> <aside class="cxy-sidebar cxy-sidebar-right" aria-label="侧边栏"> <!-- 推荐阅读 --> <div class="cxy-widget"> <h3 class="cxy-widget-title">推荐阅读</h3> <div class="cxy-recommend-grid"> <a href="https://www.vqhf.com/article/6568.html" class="cxy-recommend-item"> <div class="cxy-recommend-thumb"> <img src="https://www.vqhf.com/static/v0/images/article-6.jpg" alt="PHP 框架如何影响应用程序响应时间?"> </div> <div class="cxy-recommend-title">PHP 框架如何影响应用程序响应时间?</div> </a> <a href="https://www.vqhf.com/article/6565.html" class="cxy-recommend-item"> <div class="cxy-recommend-thumb"> <img src="https://www.vqhf.com/static/v0/images/article-6.jpg" alt="php生成N个不重复的随机数"> </div> <div class="cxy-recommend-title">php生成N个不重复的随机数</div> </a> <a href="https://www.vqhf.com/article/6562.html" class="cxy-recommend-item"> <div class="cxy-recommend-thumb"> <img src="https://www.vqhf.com/static/v0/images/article-6.jpg" alt="坛里有朋友搞过阿里巴巴API的授权请求吗解决方法"> </div> <div class="cxy-recommend-title">坛里有朋友搞过阿里巴巴API的授权请求吗解决方法</div> </a> <a href="https://www.vqhf.com/article/6557.html" class="cxy-recommend-item"> <div class="cxy-recommend-thumb"> <img src="https://www.vqhf.com/static/v0/images/article-6.jpg" alt="php图像处理常用函数与实例_PHP教程"> </div> <div class="cxy-recommend-title">php图像处理常用函数与实例_PHP教程</div> </a> </div> </div> <!-- 最新文章 --> <div class="cxy-widget"> <h3 class="cxy-widget-title">最新文章</h3> <ul class="cxy-article-list"> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6568.html" class="cxy-article-link">PHP 框架如何影响应用程序响应时间?</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6565.html" class="cxy-article-link">php生成N个不重复的随机数</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6562.html" class="cxy-article-link">坛里有朋友搞过阿里巴巴API的授权请求吗解决方法</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6557.html" class="cxy-article-link">php图像处理常用函数与实例_PHP教程</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6555.html" class="cxy-article-link">c# ConcurrentDictionary 内部是如何实现分段锁的</a> <div class="cxy-article-meta">2026-09-14</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/6550.html" class="cxy-article-link">PHP实现抓取页面与代码解析_PHP教程</a> <div class="cxy-article-meta">2026-09-14</div> </li> </ul> </div> <!-- 热门标签 --> <div class="cxy-widget"> <h3 class="cxy-widget-title">热门标签</h3> <div class="cxy-article-tag-list"> <a href="https://www.vqhf.com/tag/6675" class="cxy-article-tag-item">抓</a> <a href="https://www.vqhf.com/tag/6674" class="cxy-article-tag-item">往往</a> <a href="https://www.vqhf.com/tag/6673" class="cxy-article-tag-item">序时</a> <a href="https://www.vqhf.com/tag/6672" class="cxy-article-tag-item">程</a> <a href="https://www.vqhf.com/tag/6671" class="cxy-article-tag-item">程序时</a> <a href="https://www.vqhf.com/tag/6670" class="cxy-article-tag-item">订阅</a> <a href="https://www.vqhf.com/tag/6669" class="cxy-article-tag-item">RSS</a> <a href="https://www.vqhf.com/tag/6668" class="cxy-article-tag-item">或者</a> <a href="https://www.vqhf.com/tag/6667" class="cxy-article-tag-item">天气预报</a> <a href="https://www.vqhf.com/tag/6666" class="cxy-article-tag-item">在做</a> <a href="https://www.vqhf.com/tag/6665" class="cxy-article-tag-item">抓取</a> <a href="https://www.vqhf.com/tag/4489" class="cxy-article-tag-item">一些</a> <a href="https://www.vqhf.com/tag/3285" class="cxy-article-tag-item">页面</a> <a href="https://www.vqhf.com/tag/3212" class="cxy-article-tag-item">需要</a> <a href="https://www.vqhf.com/tag/3200" class="cxy-article-tag-item">与</a> </div> </div> </aside> </div> </div> </main> <!-- 页脚 --> <footer class="cxy-footer"> <div class="cxy-container"> <div class="cxy-footer-links" style="justify-content: center;"> <a href="https://www.vqhf.com/22.html" class="cxy-footer-link">免责声明</a> <a href="https://www.vqhf.com/23.html" class="cxy-footer-link">关于我们</a> <a href="https://www.vqhf.com/24.html" class="cxy-footer-link">联系我们</a> <a href="https://www.vqhf.com/25.html" class="cxy-footer-link">投诉删除</a> <a href="https://www.vqhf.com/26.html" class="cxy-footer-link">投稿须知</a> </div> <div class="cxy-footer-copyright" style="text-align: center; margin-top: 20px;"> Copyright 2026 趣航编程网 版权所有 <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank" style="color: inherit;">黔ICP备2025063733号</a> </div> <div style="text-align: center; margin-top: 10px; font-size: 12px; color: #999;">免责声明:本站信息来源于网络,仅供学习使用,版权归原作者所有,如有不愿意被转载的情况,请联系QQ:182111886,通知我们删除已转载的信息。</div> </div> </footer> <!-- 返回顶部按钮 --> <button class="cxy-back-to-top" id="backToTop" aria-label="返回顶部">⇧</button> <!-- 公共脚本 --> <script src="https://www.vqhf.com/static/v0/js/common.js"></script> <div class="hide"> </div> </body> <script> (function() { var al = document.createElement("script"); al.src = "/api/log?action=views&code=200&id=6550&path=http%3A%2F%2Fwww.vqhf.com%2Farticle%2F6550.html&type=archive&nonce="+Date.now(); document.body.appendChild(al); })(); </script> </html>