Home Backend Development PHP Tutorial php抓取网页的若干实现模式

php抓取网页的若干实现模式

Jun 13, 2016 am 11:55 AM
lines nbsp php string url

php抓取网页的若干实现方式

php抓取网页的若干实现方式



最近在做一个笑话平台,包含web版、安装版,由于没有笑话资源,所以就用php写了一个后台程序,每天定时从各大笑话网站抓取数据,下面整理了一些php抓取网页内容的基本方式。


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

1. file()函数    2. file_get_contents()函数  3. fopen()->fread()->fclose()模式  4.curl方式  5. fsockopen()函数 socket模式  6. 使用插件。


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

1. 正则表达式    2. PHP DOMDocument对象  3. 插件(如:PHP Simple HTML DOM Parser)

如果你对以上内容已经很了解,以下内容可以飘过……

PHP抓取页面

1. file()函数


<?php $url='';$lines_array=file($url);$lines_string=implode('',$lines_array);echo htmlspecialchars($lines_string);?>
Copy after login


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

<span style="font-size:18px;"><?php $url='';$lines_string=file_get_contents($url);echo htmlspecialchars($lines_string);?></span>
Copy after login


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

<span style="font-size:24px;"><?php $url='';$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);?></span>
Copy after login


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

<?php $url='';$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);?>
Copy after login


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);}?>
Copy after login


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

PHP解析xml(html)

1. 正则表达式:

<?php $url='';$lines_string=file_get_contents($url);eregi('<title>(.*)',$lines_string,$title);echo htmlspecialchars($title[0]);?>
Copy after login


2. PHP DOMDocument()对象
如果远程的html或xml存在语法错误,php在解析dom的时候会报错。

<?php $url='';$html=new DOMDocument();$html->loadHTMLFile($url);$title=$html->getElementsByTagName('title');echo $title->item(0)->nodeValue;?>
Copy after login


3. 插件
本文以PHP Simple HTML DOM Parser为例,进行简单介绍,simple_html_dom的语法类似jQuery,它让php操作dom,就像使用jQuery操作dom一样的简单。

<?php $url='';include_once('../simplehtmldom/simple_html_dom.php');$html=file_get_html($url);$title=$html->find('title');echo $title[0]->plaintext;?>
Copy after login




Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles