php抓取页面的几种方法详解

时间:2023-11-14 10:53:42 

在 做一些天气预报或者RSS订阅的程序时,往往需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过http请求访问url地址, 然后得到html源代码或者xml数据,得到数据我们不能直接输出,往往需要对内容进行提取,然后再进行格式化,以更加友好的方式显现出来。
下面简单说一下php抓取页面的几种方法及原理:
一、 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. 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<br />\n"
} else {
    fwrite($fp, "\n")
    echo fread($fp, 26)
    fclose($fp)


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

标签:php,抓取网页
0
投稿

猜你喜欢

  • pytest中的fixture基本用法

    2023-07-14 12:26:45
  • python清除字符串中间空格的实例讲解

    2023-12-29 22:40:24
  • ubuntu20.04配置mysql8.0的实现步骤

    2024-01-28 14:56:16
  • python实现批量转换图片为黑白

    2023-03-14 15:13:11
  • 详解mysql 使用left join添加where条件的问题分析

    2024-01-15 18:30:27
  • js模拟电脑选择多文件夹效果_选区代码

    2024-05-10 14:08:08
  • tensorflow之并行读入数据详解

    2021-09-20 14:42:30
  • Keras神经网络efficientnet模型搭建yolov3目标检测平台

    2021-10-08 11:45:33
  • 浅谈python numpy中nonzero()的用法

    2021-09-23 00:06:01
  • python爬取微信公众号文章

    2021-12-30 18:46:35
  • python识别围棋定位棋盘位置

    2023-01-09 01:49:34
  • 关于MySQL自增ID的一些小问题总结

    2024-01-22 18:52:56
  • 五个Python命令使用的小妙招分享

    2023-12-09 07:58:04
  • Python中实现字符串类型与字典类型相互转换的方法

    2021-07-01 20:37:25
  • Python区块链创建Genesis Block教程

    2022-04-17 10:44:15
  • Xml_javascript分页

    2008-09-04 14:43:00
  • django连接Mysql中已有数据库的方法详解

    2024-01-23 09:00:59
  • 解决PHP mysql_query执行超时(Fatal error: Maximum execution time …)

    2023-11-17 08:13:30
  • v语言初体验小结

    2022-09-22 01:02:51
  • python数字图像处理之基本图形的绘制

    2021-10-21 05:29:42
  • asp之家 网络编程 m.aspxhome.com