简单 HTML DOM 从标题中获取 href 和锚文本
Simple HTML DOM getting href and anchor text from within heading
对于初学者来说,这是我的代码
<?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>";
}
?>
由于该网站的链接上没有 classes,我不得不从类似的东西中提取我的信息
<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
?
我希望这个站点的链接上有一个 class,因为这当然会让这一切变得更容易。我使用这些功能没有任何问题,因为网站实际上在他们的链接上使用 classes,但看起来广告牌决定让我更难!
如果方向正确,我们将不胜感激。
注意:我的 parser_class.php
位于 here
而不是 h3
与 class title
你必须 select 锚点。所以 h3.title a
现在从那个锚点你会得到 href
和 anchor text
。为了获得 href,您可以从锚 html.
创建 SimpleXMLElement
对象
<?php
include ('parser_class.php');
$source = file_get_html('http://www.billboard.com/search/site/awards?f[0]=ss_bb_type%3Aarticle');
foreach ($source->find('h3.title a') as $anchor) {
$anch = new SimpleXMLElement($anchor);
echo "Anchor text is : ".$anch;
echo "<br>";
echo "href is : ";
echo $link_href = $anch['href'];
echo "<hr>";
}
?>
对于初学者来说,这是我的代码
<?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>";
}
?>
由于该网站的链接上没有 classes,我不得不从类似的东西中提取我的信息
<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
?
我希望这个站点的链接上有一个 class,因为这当然会让这一切变得更容易。我使用这些功能没有任何问题,因为网站实际上在他们的链接上使用 classes,但看起来广告牌决定让我更难!
如果方向正确,我们将不胜感激。
注意:我的 parser_class.php
位于 here
而不是 h3
与 class title
你必须 select 锚点。所以 h3.title a
现在从那个锚点你会得到 href
和 anchor text
。为了获得 href,您可以从锚 html.
SimpleXMLElement
对象
<?php
include ('parser_class.php');
$source = file_get_html('http://www.billboard.com/search/site/awards?f[0]=ss_bb_type%3Aarticle');
foreach ($source->find('h3.title a') as $anchor) {
$anch = new SimpleXMLElement($anchor);
echo "Anchor text is : ".$anch;
echo "<br>";
echo "href is : ";
echo $link_href = $anch['href'];
echo "<hr>";
}
?>