【发布时间】:2015-12-03 02:45:59
【问题描述】:
对于初学者,这是我拥有的代码
<?php
include ('parser_class.php');
$source = file_get_html('http://www.billboard.com/search/site/awards?f[0]=ss_bb_type%3Aarticle');
$title = $source->find('h3.title'); //getting song title
?>
<div id="awar">
<?php
if ($title){
$title = array_slice($title, 0, 10);
foreach($title as $titles){
$links = $titles->href;
$string = $titles->innertext;
//$string = (strlen($string) > 75) ? substr($string,0,72).'...' : $string;
?>
<center>
<table style="width: 100%;">
<tr>
<td style="width: 50%; text-align: left; padding-left: 5px;"><span class="song"><?php echo $string ?></span></td><td style="width: 25%; text-align: left; padding-left: 5px;"><a href="http://www.billboard.com<?php echo $links ?>" class="download">Read Article</a></td>
</tr>
</table>
</center>
<hr class="betw" />
<?php
}
}
else{
echo"<p class='song'>No Articles Found</p>";
}
?>
由于该网站的链接上没有类,我不得不从类似的东西中提取我的信息
<h3 class="title">
<a href="/articles/columns/country/6784891/lady-antebellum-charles-kelley-steps-out-on-his-own">Lady Antebellum's Charles Kelley Steps Out On His Own In New York City</a>
</h3>
呼叫innertext 我得到了h3 中的所有内容
我需要弄清楚如何从h3 中分别获取href 和anchor text
有没有办法从innertext 中获取href,然后是href 的innertext?
我希望这个网站在他们的链接上有一个类,因为这当然会让这件事变得更容易。我已经毫无问题地使用了这些功能,因为网站实际上在其链接上使用了类,但看起来广告牌决定让我的事情变得更难!
我们将不胜感激。
注意:我的parser_class.php 是位于here 的那个
【问题讨论】:
标签: php dom html-parsing