How to get all urls from page (php)

前端 未结 3 1352
逝去的感伤
逝去的感伤 2020-12-29 17:30

I have a page with urls with descriptions listed one under another (something like bookmarks/list of sites). How do I use php to get all urls from that page and write them t

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-29 17:51

    Another way

    $url = "http://wwww.somewhere.com";
    
    $html = file_get_contents($url);
    
    $doc = new DOMDocument();
    $doc->loadHTML($html); //helps if html is well formed and has proper use of html entities!
    
    $xpath = new DOMXpath($doc);
    
    $nodes = $xpath->query('//a');
    
    foreach($nodes as $node) {
        var_dump($node->getAttribute('href'));
    }
    

提交回复
热议问题