从网页上刮信息
问题描述:
如何从此html页面获取信息(http://linkWeb.com,标题和http://link.pdf)?从网页上刮信息
<div class="title-download">
<div id="01divTitle" class="title">
<h3>
<a id="01Title" onmousedown="" href="http://linkWeb.com">Titles</a>
<span id="01LbCitation" class="citation">(<a id="01Citation" href="http://citation.com">Citations</a>)</span></h3>
</div>
<div id="01downloadDiv" class="download">
<a id="01_downloadIcon" title="http://link.pdf" onmousedown="" target=""><img id="ctl01_icon" class="small-icon";" /></a>
</div>
</div>
我试过但它只返回标题。我没有意识到之前的simple_tml_dom。请帮帮我。谢谢:)
<?php
include 'simple_html_dom.php';
set_time_limit(0);
$url ='http://libra.msra.cn/Search?query=data%20mining&s=0';
$html = file_get_html($url) or die ('invalid url');
foreach($html->find('div[class=title-download]') as $webLink){
echo $webLink->plaintext.'<br>';
echo $webLink->href.'<br>';
}
foreach($html->find('div[class=download]') as $Link2){
echo $webLink2->href.'<br>';
}
?>
答
废料的标题和URL使用此代码:
foreach($html->find('span[class=citation]') as $link){
$link = $link->prev_sibling();
echo $link->plaintext.'<br>';
echo $link->href.'<br>';
}
和报废类的下载网址,使用@zigomir :)
foreach($html->find('.download a') as $link){
echo $link->title.'<br>';
}
答
我认为你需要选择里面有级冠军下载DIV的一个元素。至少有资料称,它选择如jQuery(http://simplehtmldom.sourceforge.net/)
试试这样说:
$html = file_get_html($url) or die ('invalid url');
foreach($html->find('.title a') as $webLink){
echo $webLink->plaintext.'<br>';
echo $webLink->href.'<br>';
}
foreach($html->find('.download a') as $link){
echo $link->title.'<br>';
}
答
使用的libxml解析HTML和使用的XPath指定的元素或元素属性你想要的。
随着你的foreach第二次给出的答案寻找一个http://link.pdf,它是用属性“title”指定的,而不是用“href”指定的...... – zigomir 2012-07-21 02:06:11
@zigomir哦,是的!感谢您的更正! :) – bruine 2012-07-22 01:24:30