Selecting a specific div from a extern webpage using CURL

前端 未结 3 1814
执念已碎
执念已碎 2021-02-15 07:06

Hi can anyone help me how to select a specific div from the content of a webpage.

Let\'s say i want to get the div with id=\"wrapper_content\" from webpage

相关标签:
3条回答
  • 2021-02-15 07:55

    check our hpricot, it lets you elegantly select sections

    first you would use curl to get the document, then use hpricot to get the part you need

    0 讨论(0)
  • 2021-02-15 07:58
    include('simple_html_dom.php');
    $html = str_get_html($file_contents);
    $elem = $html->find('div[id=wrapper_content]', 0);
    

    Download simple_html_dom.php

    0 讨论(0)
  • 2021-02-15 08:09

    HTML isn't regular, so you shouldn't use regex. Instead I would recommend a HTML Parser such as Simple HTML DOM or DOM

    If you were going to use Simple HTML DOM you would do something like the following:

    $html = str_get_html($file_contents);
    $elem = $html->find('div[id=wrapper_content]', 0);
    

    Even if you used regex your code still wouldn't work correctly. You need to get the contents of the page before you can use regex.

    //wrong
    if(!preg_match($s_searchFor, $ch)){
        $file_contents = curl_exec($ch);
    }
    
    //right
    $file_contents = curl_exec($ch); //get the page contents
    preg_match($s_searchFor, $file_contents, $matches); //match the element
    $file_contents = $matches[0]; //set the file_contents var to the matched elements
    
    0 讨论(0)
提交回复
热议问题