Retrieve elements with xpath and DOMDocument

后端 未结 2 811
旧时难觅i
旧时难觅i 2021-02-09 01:33

I have a list of ads in the html code below. What I need is a PHP loop to get the folowing elements for each ad:

  1. ad URL (href attribute of t
2条回答
  •  情话喂你
    2021-02-09 02:04

    for other elements, you just do the same:

    foreach ($ls_ads as $ad) {
        $ad_url = $ad->getAttribute('href');
        print("AD URL : $ad_url");
        $ad_Doc = new DOMDocument();
        $ad_Doc->documentElement->appendChild($ad_Doc->importNode($ad));
        $xpath = new DOMXPath($ad_Doc);
        $img_src = $xpath->query("//img[@src]");
        $title = $xpath->query("//div[@class='title']");
    }
    

提交回复
热议问题