php爬虫采集技术,轻松抓取网页!含源码

电脑杂谈  发布时间:2020-02-27 08:01:18  来源:网络整理

php curl 爬虫_php 爬虫代码_php 爬虫

各位同学大家好,今天给你们带给的是

php爬虫采集类-phpQuery:支持抓取网页,非常强大的php类库!

它是一款基于PHP服务端开源的项目,可以使PHPer轻松处理DOM文档内容php 爬虫代码,比如可以获得某网站的头条信息。更有意思的是,它运用了jQuery的观念,可以像使用jQuery一样处理页面内容,获取你想要的页面信息。

好了php 爬虫代码,废话不多说,上源码!

<?php

/**

* phpQuery is a server-side, chainable, CSS3 selector driven

* Document Object Model (DOM) API based on jQuery Java Library.

*

* @version 0.9.5

* @link

* @link

* @link

* @author Tobiasz Cudnik <tobiasz.cudnik/gmail.com>

* @license MIT License

* @package phpQuery

*/

// class names for instanceof

// TODO move them as class constants into phpQuery

define('DOMDOCUMENT', 'DOMDocument');

define('DOMELEMENT', 'DOMElement');

define('DOMNODELIST', 'DOMNodeList');

define('DOMNODE', 'DOMNode');

/**

* DOMEvent class.

*

* Based on

* @link :event

* @author Tobiasz Cudnik <tobiasz.cudnik/gmail.com>

* @package phpQuery

* @todo implement ArrayAccess ?

*/

class DOMEvent {

/**

* Returns a boolean indicating whether the event bubbles up through the DOM or not.

*

* @var unknown_type

*/

public $bubbles = true;

/**

* Returns a boolean indicating whether the event is cancelable.

*

* @var unknown_type

*/

public $cancelable = true;

/**

* Returns a reference to the currently registered target for the event.

*

* @var unknown_type

*/

public $currentTarget;

/**

* Returns detail about the event, depending on the type of event.

*

* @var unknown_type

* @link

*/

public $detail; // ???

/**

* Used to indicate which phase of the event flow is currently being evaluated.

php 爬虫_php curl 爬虫_php 爬虫代码

*

* NOT IMPLEMENTED

*

* @var unknown_type

* @link

*/

public $eventPhase; // ???

/**

* The explicit original target of the event (Mozilla-specific).

*

* NOT IMPLEMENTED

*

* @var unknown_type

*/

public $explicitOriginalTarget; // moz only

/**

* The original target of the event, before any retargetings (Mozilla-specific).

*

* NOT IMPLEMENTED

*

* @var unknown_type

*/

public $originalTarget; // moz only

/**

* Identifies a secondary target for the event.

*

* @var unknown_type

*/

public $relatedTarget;

/**

* Returns a reference to the target to which the event was originally dispatched.

*

* @var unknown_type

*/

public $target;

/**

* Returns the time that the event was created.

*

* @var unknown_type

*/

public $timeStamp;

/**

* Returns the name of the event (case-insensitive).

*/

public $type;

public $runDefault = true;

public $data = null;

public function __construct($data) {

foreach($data as $k => $v) {

$this->$k = $v;

}

if (! $this->timeStamp)

$this->timeStamp = time();

}

/**

* Cancels the event (if it is cancelable).

*

*/

public function preventDefault() {

$this->runDefault = false;

}

php curl 爬虫_php 爬虫代码_php 爬虫

/**

* Stops the propagation of events further along in the DOM.

*

*/

public function stopPropagation() {

$this->bubbles = false;

}

}

/**

* DOMDocumentWrapper class simplifies work with DOMDocument.

*

* Know bug:

* - in XHTML fragments, <br /> changes to <br clear="none" />

*

* @todo check XML catalogs compatibility

* @author Tobiasz Cudnik <tobiasz.cudnik/gmail.com>

* @package phpQuery

*/

class DOMDocumentWrapper {

/**

* @var DOMDocument

*/

public $document;

public $id;

/**

* @todo Rewrite as method and quess if null.

* @var unknown_type

*/

public $contentType = '';

public $xpath;

public $uuid = 0;

public $data = array();

public $dataNodes = array();

public $events = array();

public $eventsNodes = array();

public $eventsGlobal = array();

/**

* @TODO iframes support

* @var unknown_type

*/

public $frames = array();

/**

* Document root, by default equals to document itself.

* Used by documentFragments.

*

* @var DOMNode

*/

public $root;

public $isDocumentFragment;

public $isXML = false;

public $isXHTML = false;

public $isHTML = false;

public $charset;

public function __construct($markup = null, $contentType = null, $newDocumentID = null) {

if (isset($markup))

$this->load($markup, $contentType, $newDocumentID);

$this->id = $newDocumentID

? $newDocumentID

: md5(microtime());

}

public function load($markup, $contentType = null, $newDocumentID = null) {

php 爬虫_php 爬虫代码_php curl 爬虫

// phpQuery::$documents[$id] = $this;

$this->contentType = strtolower($contentType);

if ($markup instanceof DOMDOCUMENT) {

$this->document = $markup;

$this->root = $this->document;

$this->charset = $this->document->encoding;

// TODO isDocumentFragment

$loaded = true;

} else {

$loaded = $this->loadMarkup($markup);

}

if ($loaded) {

// $this->document->formatOutput = true;

$this->document->preserveWhiteSpace = true;

$this->xpath = new DOMXPath($this->document);

$this->afterMarkupLoad();

return true;

// remember last loaded document

// return phpQuery::selectDocument($id);

}

return false;

}

protected function afterMarkupLoad() {

if ($this->isXHTML) {

$this->xpath->registerNamespace("html", "http://www.w3.org/1999/xhtml");

}

}

protected function loadMarkup($markup) {

$loaded = false;

if ($this->contentType) {

self::debug("Load markup for content type {$this->contentType}");

// content determined by contentType

list($contentType, $charset) = $this->contentTypeToArray($this->contentType);


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/ruanjian/article-142239-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 张汇
      张汇

      只是一种在军事上表现一种姿态

    热点图片
    拼命载入中...