使用PHP刮擦网站"简单的HTML Dom Parser"

时间:2015-11-20 08:14:33

标签: php html dom

我无法弄清楚如何使用PHP Simple HTML DOM Parser从网站中提取信息。

require('simple_html_dom.php');
$html = file_get_html('https://example.com');

$ret = array();
foreach($html->find(".project-card-mini-wrap") as $element)  { 
   echo $element;   
}

$element的输出是:

<div class="project-card-mini-wrap"> 
<a class="project_item block mb2 green-dark"    href="/projects/andrewkostirev/kostirev-the-real-you">
<div class="project_thumbnail hover-group border border-box mb1"> 
     <img alt="Project image" class="hover-zoomin fit" src="https://ksr-ugc.imgix.net/projects/2123706/photo-original.png?v=1444253259&amp;w=218&amp;h=162&amp;fit=crop&amp;auto=format&amp;q=92&amp;s=9d6c437e96b720dce82fc9b598b3e8ae" /> 
    <div class="funding_tag highlight">10 days to go</div> 
   <div class="hover-zoomout bg-green-90"> 
   <p class="white p2 h5">A clothing brand like never seen before</p> 
</div> 
</div> 
<div class="project_name h5 bold"> KOSTIREV - THE REAL YOU </div>
</a>
</div>

这是我想从网站上提取的信息:
1:链接href
2:图像src
3:项目名称

3 个答案:

答案 0 :(得分:1)

希望这会为您以及 PHP Simple HTML DOM Parser

的其他用户提供一些见解
foreach($html->find(".project-card-mini-wrap") as $element)  { 
   echo "Project name: ",$element->find('.project_name',0)->innertext,"<br/>\n";
   echo "Image source: ",$element->find('img',0)->src,"<br/>\n";
   echo "Link: ",$element->find('a',0)->href,"<br/>\n";
}

生成此输出:

Project name: KOSTIREV - THE REAL YOU 
Image source: https://ksr-ugc.imgix.net/projects/2123706/photo-original.png?v=1444253259&w=218&h=162&fit=crop&auto=format&q=92&s=9d6c437e96b720dce82fc9b598b3e8ae
Link: /projects/andrewkostirev/kostirev-the-real-you

答案 1 :(得分:0)

我试过这个并且它有效,谢谢你的帮助!这是我用primewire.ag作为例子做的事情....这里的目标是提取给定页面的所有链接。

<?php

require('simple_html_dom.php');

// Create DOM from URL or file
$html = file_get_html('http://www.primewire.ag/watch-2805774-Star-Wars-The-Last-Jedi-online-free');


// Find All Movie Links
$linkPrefix = 'http://primewire.ag';
$linkClass;
foreach($html->find(".movie_version_link") as $linkClass)  {
    echo "Link: ",$linkPrefix,$linkClass->find('a',0)->href,"<br/>\n";

}
?>

答案 2 :(得分:0)

这也是一个很好的通过 HTML 抓取和遍历的库

https://github.com/paquettg/php-html-parser