简体   繁体   English

使用simpleHTMLDOM仅抓取特定的div类将抓取整个页面

[英]Using simpleHTMLDOM to only grab a specific div class grabs the entire page instead

Hello Stackoverflow I'm trying to use the following library simplehtmldom.sourceforge.net 您好Stackoverflow我正在尝试使用以下库simplehtmldom.sourceforge.net

The purpose of this little script is to grab the StackOverflow Logo and echo it. 这个小脚本的目的是获取StackOverflow徽标并对其进行回显。 But for some strange reason it grabs every DOM element instead. 但是出于某种奇怪的原因,它却获取了每个DOM元素。 Any idea what i'm doing wrong here? 任何想法我在这里做错了吗?

<?php
include('simple_html_dom.php');

$request_url = 'http://stackoverflow.com/';
$html = file_get_html($request_url);

$element = $html->find('div[id=hlogo]');

echo $html->save($element);

Thank you in advance for taking your time to read this! 预先感谢您抽出宝贵的时间阅读本文!

$html->find returns an array in the form that you're using it, so you need to access the first element of the array to get the results: $html->find以您使用的形式返回一个数组,因此您需要访问该数组的第一个元素以获取结果:

include('simple_html_dom.php');

$html = file_get_html('http://stackoverflow.com');
$logo = $html->find('#hlogo'); // find the id hlogo

echo $logo[0];

# prints out
# <div id="hlogo"> <a href="/"> Stack Overflow </a> </div>

You're also using the save function wrong; 您还使用了错误的save功能; from the docs: 从文档:

// Dumps the internal DOM tree back into string 
$str = $html->save();

// Dumps the internal DOM tree back into a file 
$html->save('result.htm');

You're getting the whole page because $html contains the whole DOM! 您将获得整个页面,因为$html包含整个DOM!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM