如何使用PHP Simple HTML DOM Parser来抓取动态数据

时间:2014-07-07 16:50:38

标签: php jquery html dom

首先让我说我已阅读过多次"报废"在这里的线程,没有一个对我有帮助。我也在互联网上查了几天,现在我越来越接近电线我希望有人可以为我解释这个问题。

我使用PHP Simple HTML DOM Parser从页面中抓取一些数据。我正在使用的网址提供动态内容,我似乎没有任何工作来提取内容。我需要从<tr id="0" class="ui-widget-content jqgrow ui-row-ltr" role="row"><tr id="9" class="ui-widget-content jqgrow ui-row-ltr" role="row">抓取文字(普通),我觉得一旦我上班,我就能得到其他人。因为当页面加载时这个信息实际上不在页面上,而是在页面加载后进入折叠状态,我处于rutt状态。

话虽如此,这就是我的尝试:

echo file_get_html('http://sheriffclevelandcounty.com/p2c/jailinmates.aspx')->plaintext; 

以上将向我展示一切但我需要的信息,如下:

我也尝试使用IMDb插件中的示例并根据我的需要进行修改,就是这样:

// Defining the basic cURL function
    function curl($url) {
        // Assigning cURL options to an array
        $options = Array(
            CURLOPT_RETURNTRANSFER => TRUE,  // Setting cURL's option to return the webpage data
            CURLOPT_FOLLOWLOCATION => TRUE,  // Setting cURL to follow 'location' HTTP headers
            CURLOPT_AUTOREFERER => TRUE, // Automatically set the referer where following 'location' HTTP headers
            CURLOPT_CONNECTTIMEOUT => 120,   // Setting the amount of time (in seconds) before the request times out
            CURLOPT_TIMEOUT => 120,  // Setting the maximum amount of time for cURL to execute queries
            CURLOPT_MAXREDIRS => 10, // Setting the maximum number of redirections to follow
            CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8",  // Setting the useragent
            CURLOPT_URL => $url, // Setting cURL's URL option with the $url variable passed into the function
        );

        $ch = curl_init();  // Initialising cURL
        curl_setopt_array($ch, $options);   // Setting cURL's options using the previously assigned array data in $options
        $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
        curl_close($ch);    // Closing cURL
        return $data;   // Returning the data from the function
    }

     // Defining the basic scraping function
    function scrape_between($data, $start, $end){
        $data = stristr($data, $start); // Stripping all data from before $start
        $data = substr($data, strlen($start));  // Stripping $start
        $stop = stripos($data, $end);   // Getting the position of the $end of the data to scrape
        $data = substr($data, 0, $stop);    // Stripping all data from after and including the $end of the data to scrape
        return $data;   // Returning the scraped data from the function
    }

    $scraped_page = curl("http://sheriffclevelandcounty.com/p2c/jailinmates.aspx");    // Downloading IMDB home page to variable $scraped_page
    $scraped_data = scrape_between($scraped_page, '<table id="tblII" class="ui-jqgrid-btable" cellspacing="0" cellpadding="0" border="0" role="grid" aria-multiselectable="false" aria-labelledby="gbox_tblII" style="width: 456px;">', '</table>');   // Scraping downloaded dara in $scraped_page for content between <title> and </title> tags

    echo $scraped_data; // Echoing $scraped data, should show "The Internet Movie Database (IMDb)"

当然这些都不起作用,所以我的问题是:如何使用PHP Simple DOM Parser获取页面加载后加载的动态内容?是可能还是我完全走错了?

1 个答案:

答案 0 :(得分:0)

我知道你需要jqgrid中的动态数据。为此,您可以使用帖子URL,作为响应提供数据。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://sheriffclevelandcounty.com/p2c/jqHandler.ashx?op=s");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, array(
'rows'=>10000, //Here you can specify how many records you want
't'=>'ii'
    ));
$output = curl_exec($ch);
curl_close($ch);
echo "<pre>";
print_r(json_decode($output));