2012-07-21 179 views
0

如何从此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>';  
} 

?> 
+1

随着你的foreach第二次给出的答案寻找一个http://link.pdf,它是用属性“title”指定的,而不是用“href”指定的...... – zigomir 2012-07-21 02:06:11

+0

@zigomir哦,是的!感谢您的更正! :) – bruine 2012-07-22 01:24:30

回答

0

废料的标题和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>';  
} 
2

我认为你需要选择里面有级冠军下载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>';  
} 
+0

问题是该html页面的内容在每个结果中都有不同的id。例如,第二个结果必须是'id =“02Title'和'id = 02_downloadIcon' – bruine 2012-07-21 02:10:29

+1

,那么你应该按照类来选择:'.title a'。我也编辑了我的答案。 – zigomir 2012-07-21 14:20:42

+0

哦,是的,谢谢你呢!但是,它也会把引用记录下来,我只需要取消标题和URL,我已经找到了取消标题和URL的方法,查看我的答案。谢谢分享!让我明白如何访问HTML元素:) – bruine 2012-07-22 01:16:36

0

使用的libxml解析HTML和使用的XPath指定的元素或元素属性你想要的。