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);?>
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>
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>
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);?>
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解析xml(html)
1. 正则表达式:
<?php $url='';$lines_string=file_get_contents($url);eregi('<title>(.*)',$lines_string,$title);echo htmlspecialchars($title[0]);?>
2. PHP DOMDocument()对象
如果远程的html或xml存在语法错误,php在解析dom的时候会报错。
<?php $url='';$html=new DOMDocument();$html->loadHTMLFile($url);$title=$html->getElementsByTagName('title');echo $title->item(0)->nodeValue;?>
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;?>

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











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,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

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.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.
