一个特殊HTML元素的PHP爬虫程序
我们有一个简单的HTML页面(用于测试!):一个特殊HTML元素的PHP爬虫程序,php,html,Php,Html,我们有一个简单的HTML页面(用于测试!): 一个 两个 三 不 不 所以,我需要一个非常简单的php代码来爬网。 我想要爬网的东西是,我想要把“一”、“二”、“三”放到一个php数组中,我需要把所有的东西都爬网到“我的”类中。我不想上其他的课 您应该使用DOMDocument类 <?php $html='<html> <body> <div class="my"> One </div> <div class="my"> T
一个
两个
三
不
不
所以,我需要一个非常简单的php代码来爬网。
我想要爬网的东西是,我想要把“一”、“二”、“三”放到一个php数组中,我需要把所有的东西都爬网到“我的”类中。我不想上其他的课 您应该使用
DOMDocument
类
<?php
$html='<html>
<body>
<div class="my"> One </div>
<div class="my"> Two </div>
<div class="my"> Three </div>
<div class="other"> NO </div>
<div class="other2"> NO </div>
</body>
</html>';
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('div') as $tag) {
if ($tag->getAttribute('class') === 'my') {
echo $tag->nodeValue; // to get the content in between of tags...
}
}
尝试一下,您可以使用
xpath
获得结果
$html = '<html>
<body>
<div class="my"> One </div>
<div class="my"> Two </div>
<div class="my"> Three </div>
<div class="other"> NO </div>
<div class="other2"> NO </div>
</body>
</html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$tags = $xpath->query('//div[@class="my"]');
foreach ($tags as $tag) {
$node_value = trim($tag->nodeValue);
echo $node_value."<br/>";
}
$html='1!'
一个
两个
三
不
不
';
$dom=新的DOMDocument();
$dom->loadHTML($html);
$xpath=newdomxpath($dom);
$tags=$xpath->query('//div[@class=“my”]');
foreach($tags作为$tag){
$node_value=trim($tag->nodeValue);
回显$node_值。“
”;
}
使用
非常感谢。但这是一个问题。为什么xpath更好?xpath使用给定条件直接搜索html,就像我们使用
类“my”
搜索div一样。没有其他部门会参与结果。这个过程将由那边的xpath完成。我们不需要在php页面上操纵它。@SatishSharma谢谢亲爱的。只是一个小问题。如果我想抓取一个外部HTML页面,我如何将该页面的地址输入到您的代码中代码>谢谢@Donald Duck
One Two Three
$html = '<html>
<body>
<div class="my"> One </div>
<div class="my"> Two </div>
<div class="my"> Three </div>
<div class="other"> NO </div>
<div class="other2"> NO </div>
</body>
</html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$tags = $xpath->query('//div[@class="my"]');
foreach ($tags as $tag) {
$node_value = trim($tag->nodeValue);
echo $node_value."<br/>";
}
<?php /* crawlUrlElement.php */
/**
* Created by PhpStorm.
* User: admxxi@gmail.com
* Date: 15/03/2017
* Time: 15:01
*/
require("simple_html_dom.php");
function crawlUrlElement($url, $search){
$crawlOptions = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "samplebot", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 5, // stop after 5 redirects
);
//-- Curl Start --
$curlObject = curl_init($url);
curl_setopt_array($curlObject,$crawlOptions);
$webPageContent = curl_exec($curlObject);
$errorNumber = curl_errno($curlObject);
curl_close($curlObject);
//-- Curl End --
// Create DOM from URL or file
$html = file_get_html($webPageContent);
// Find all images
foreach($html->find($search) as $element){
// print_r($element);
return (string)$element;
}
}
// echo var_dump(crawlUrlElement('http://www.google.com','body'));
echo var_dump(crawlUrlElement('http://www.google.com','#hplogo'));
?>
function file_get_html($contents, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)
{
// We DO force the tags to be terminated.
$dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);
// For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done.
// $contents = file_get_contents($url, $use_include_path, $context, $offset);
}