How to get Content of Remote HTML page

前端 未结 1 1679
遥遥无期
遥遥无期 2020-12-05 20:53

I want to get remote html contents that on \"li\" that with a spacific Class name and Children of em using divs.

My remote content is Like this

<
相关标签:
1条回答
  • 2020-12-05 21:29

    Use CURL to read the remote URL to fetch the HTML.

    $url = "http://www.example.com";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $output = curl_exec($curl);
    curl_close($curl);
    

    Then use PHP's DOM object model to parse the HTML.

    For example to fetch all <h1> tags from the source,

    $DOM = new DOMDocument;
    $DOM->loadHTML( $output);
    
    //get all H1
    $items = $DOM->getElementsByTagName('h1');
    
    //display all H1 text
     for ($i = 0; $i < $items->length; $i++)
            echo $items->item($i)->nodeValue . "<br/>";
    
    0 讨论(0)
提交回复
热议问题