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

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

switch($contentType) {

case 'text/html':

phpQuery::debug("Loading HTML, content type '{$this->contentType}'");

$loaded = $this->loadMarkupHTML($markup, $charset);

break;

case 'text/xml':

case 'application/xhtml+xml':

phpQuery::debug("Loading XML, content type '{$this->contentType}'");

$loaded = $this->loadMarkupXML($markup, $charset);

break;

default:

// for feeds or anything that sometimes doesn't use text/xml

if (strpos('xml', $this->contentType) !== false) {

phpQuery::debug("Loading XML, content type '{$this->contentType}'");

$loaded = $this->loadMarkupXML($markup, $charset);

} else

phpQuery::debug("Could not determine document type from content type '{$this->contentType}'");

}

} else {

// content type autodetection

if ($this->isXML($markup)) {

phpQuery::debug("Loading XML, isXML() == true");

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

if (! $loaded && $this->isXHTML) {

phpQuery::debug('Loading as XML failed, trying to load as HTML, isXHTML == true');

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

}

} else {

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

phpQuery::debug("Loading HTML, isXML() == false");

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

}

}

return $loaded;

}

protected function loadMarkupReset() {

$this->isXML = $this->isXHTML = $this->isHTML = false;

}

protected function documentCreate($charset, $version = '1.0') {

if (! $version)

$version = '1.0';

$this->document = new DOMDocument($version, $charset);

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

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

$this->document->formatOutput = true;

$this->document->preserveWhiteSpace = true;

}

protected function loadMarkupHTML($markup, $requestedCharset = null) {

if (phpQuery::$debug)

phpQuery::debug('Full markup load (HTML): '.substr($markup, 0, 250));

$this->loadMarkupReset();

$this->isHTML = true;

if (!isset($this->isDocumentFragment))

$this->isDocumentFragment = self::isDocumentFragmentHTML($markup);

$charset = null;

$documentCharset = $this->charsetFromHTML($markup);

$addDocumentCharset = false;

if ($documentCharset) {

$charset = $documentCharset;

$markup = $this->charsetFixHTML($markup);

} else if ($requestedCharset) {

$charset = $requestedCharset;

}

if (! $charset)

$charset = phpQuery::$defaultCharset;

// HTTP 1.1 says that the default charset is ISO-8859-1

// @see

if (! $documentCharset) {

$documentCharset = 'ISO-8859-1';

$addDocumentCharset = true;

}

// Should be careful here, still need 'magic encoding detection' since lots of pages have other 'default encoding'

// Worse, some pages can have mixed encodings... we'll try not to worry about that

$requestedCharset = strtoupper($requestedCharset);

$documentCharset = strtoupper($documentCharset);

phpQuery::debug("DOC: $documentCharset REQ: $requestedCharset");

if ($requestedCharset && $documentCharset && $requestedCharset !== $documentCharset) {

phpQuery::debug("CHARSET CONVERT");

// Document Encoding Conversion

//

if (function_exists('mb_detect_encoding')) {

$possibleCharsets = array($documentCharset, $requestedCharset, 'AUTO');

$docEncoding = mb_detect_encoding($markup, implode(', ', $possibleCharsets));

if (! $docEncoding)

$docEncoding = $documentCharset; // ok trust the document

phpQuery::debug("DETECTED '$docEncoding'");

// Detected does not match what document says...

if ($docEncoding !== $documentCharset) {

// Tricky..

}返回搜狐,查看更多


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

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

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