douzhaishan5462 2016-07-12 23:37
浏览 392

如何使用xpath提取解析href,src,alt

i need to find the image link and a href . show href,img src, and alt tag.

here my code

$xpath = new DOMXPath('http,://.....');


foreach ($xpath->query('//a[@href]//img') as $img) {


echo '<a href=' .'"' .$img['href'] .'"' .'/>' .'<img src="' .$img['src']    .'"' .'alt="' .$img['alt'] .'"/>' .'</a>';



Catchable fatal error: Argument 1 passed to DOMXPath::__construct() must be an     
instance of DOMDocument, string given, called in

Can you help me??

  • 写回答

1条回答 默认 最新

  • donglu3184 2016-07-12 23:43
    关注

    You can use the following code

    $dom = new DOMDocument();
    libxml_use_internal_errors(true);
    $dom->loadHTMLFile('http://example.com/');
    
    $xpath = new DOMXpath($dom);
    

    then use xpath to do whatever you want to do with it

    Edited to get image src from link

    # get the images inside a link
    foreach ($xpath->query('//a[@href]//img') as $img) {
    
        # find all the links and images       
        for ($link = $img; $link->tagName !== 'a'; $link = $link->parentNode);
    
        $output[] = array(
            'href' => $link->getAttribute('href'),
            'src'  => $img->getAttribute('src'),
            'alt'  => $img->getAttribute('alt'),
        );
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型