Home Backend Development PHP Tutorial 可否帮忙写一个单页页的PHP采集程序,并附下实例

可否帮忙写一个单页页的PHP采集程序,并附下实例

Jun 13, 2016 pm 12:59 PM
curl html nbsp php server

可否帮忙写一个单页页的PHP采集程序,并附上实例
比方说,我要采集这个页面:http://news.163.com/12/0613/20/83TJ7PA700014JB6.html

要求:
采集标题
采集正文

谢谢!
------解决方案--------------------
首先去http://simplehtmldom.sourceforge.net/index.htm(点击Download latest version form Sourceforge.)下载一个simple_html_dom.php,傻瓜式的正则,另官网上有详细教程,很容易看懂。

<br />
header("Content-type: text/html; charset=gb2312");<br />
require dirname(__FILE__) . '/simple_html_dom.php';<br />
$ch = curl_init();<br />
curl_setopt($ch, CURLOPT_URL, 'http://news.163.com/12/0613/20/83TJ7PA700014JB6.html');<br />
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br />
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');<br />
$htmls = curl_exec($ch);<br />
curl_close($ch);<br />
$html = str_get_html($htmls);<br />
foreach($html->find('#h1title') as $title){ <br />
       echo strip_tags($title).'<br />';//标题<br />
}<br />
foreach($html->find('#endText') as $content){ <br />
    echo strip_tags($content);//正文<br />
}
Copy after login


注:curl模拟Mozilla浏览器抓取,比file_get_contents()解析速度更快。strip_tags() 用以去除那个邪恶的iframe广告,反正你需要的是“采集标题”和“采集正文”冗余的html标签对你无用。
------解决方案--------------------

PHP获取QQ邮箱好友列表的方法:
本文为大家介绍有关如何运用PHP获取QQ邮箱好友的方法。PHP有一个扩展是curl扩展,该扩展一般用于采集数据。但是也可以实现模拟登陆,通过模拟登陆来登陆QQ邮箱,再利用curl的采集来获取邮箱的好友列表。以上是实现原理,理解袁莉以后我们来看实现过程。具体的PHP获取QQ邮箱好友的代码如下:

1. 2.class QQHttp {  
3.    var $cookie = '';  
4.    function __cunstrut() {  
5.    }  
6.    function makeForm() {  
7.        $form = array(  
8.            'url' => "http://mail.qq.com/cgi-bin/loginpage",  
9.        );  
10.        $data = $this->curlFunc($form);  
11.        preg_match('/name="ts"svalue="(d+)"/',$data['html'], $tspre);  
12.        $ts = $tspre[1];  
13.        preg_match('/action="http://(md+).mail.qq.com/',$data['html'], $server);  
14.        $server_no = $server[1];  
15.        /*  login.html 载入 */  
16.        $html = file_get_contents(dirname(__FILE__).'/login.htm');  
17.        $html = str_replace('{_ts_}',$ts, $html);  
18.        $html = str_replace('{_server_no_}',$server_no, $html);  
19.        return $html;  
20.    }  
21.    function curlFunc($array)  
22.    {  
23.        $ch = curl_init();  
24.        curl_setopt($ch, CURLOPT_URL, $array['url']);  
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.

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles