使用PHP写采集程序时用到的几种方法及碰到的常见问题
此文档适合PHP初学者学习对网页采集的理解和应用。
使用PHP写采集程序时用到的几种方法及碰到的常见问题:
1、file_get_contents($url);
使用 file_get_contents 进行采集的使用实例如下:
$url = "http://www.php.cn";
$con = file_get_contents($url); //采集到的内容将存储到$con里面
2、使用 curl 来进行采集
使用 curl 来采集数据比 file_get_contents 更加灵活,现在很多网页做了防采集的措施,如果直接使用 file_get_contents 来进行采集的话,将无法成功
但是使用 curl 将没有这种问题,curl 可以模拟浏览器信息进行采集。
以下是使用 curl 进行采集的实例,其中 curl_setopt 是常用到的几项设置,请根据需要选择。
$url = "http://www.php.cn";
$useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
$header = array('Accept-Language: zh-cn','Connection: Keep-Alive','Cache-Control: no-cache');
$ch = curl_init(); //初始化 curl
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header); //模拟浏览器的头信息
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); //模拟浏览器的信息
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); //是否保存采集内容
curl_setopt($ch, CURLOPT_TIMEOUT, 60); //curl允许执行的最大时间,单位是秒
curl_setopt($ch, CURLOPT_URL, $url); //要采集的网址
curl_setopt($ch, CURLOPT_HEADER, 1); //是否要保存头信息
$con = curl_exec($ch); //采集到的内容将存储到$con里面
3、使用网上非常流行的snoopy来进行采集,这是一个非常强大的采集插件,并且它的使用非常方便,你也可以在里面设置agent来模拟浏览器信息。
使用实例:
require('Snoopy.class.php'); //引入snoopy的类文件
$snoopy = new Snoopy; //初始化snoopy类
$url = "http://www.php.cn";
$snoopy->fetch($url); //开始采集内容
$con = $snoopy->results; //保存采集内容到$con
说明:设置agent是在 Snoopy.class.php 文件的第45行,请在该文件中搜索 “var $agent” (引号中的内容)。浏览器内容你可以使用PHP来获得,
使用 echo $_SERVER['HTTP_USER_AGENT']; 可以得到浏览器信息,将echo出来的内容复制到agent里面就可以了。
上面
组合使用,这几个例子都只是最基本的采集代码,在运用的时候你需要根据个人需求添加相应的正则来提取所需的内容。
我在做采集程序的时候碰到的几个问题主要有以下几个:
1.PHP程序的30秒的超时问题,解决的办法是在采集代码之前加 set_time_limit(0); 0代表不限制
2.采集的时候返回结果为403,此时你需要检查自己的代码是否模拟了浏览器信息

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
