3

我正在尝试将这个页面的标签剥离到可以获取页码列表的程度。所以我可以弄清楚我的 curl 程序继续抓取页面的最高页码是多少。现在,我可以将标签剥离到可以获取数字的程度,但我不知道如何分隔每个数字,因此我可以看到最高页码是多少。

我收到的当前返回值为

12

这是我的代码:

<?php
// Defining the basic pruning 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
}

ob_start();
?>
<span class="current">1</span><a href="javascript:__doPostBack('ctl00$phCenterColumn$motoSearchResults$gvCatalog$ctl01$ctl03','')">2</a><a href="javascript:__doPostBack('ctl00$phCenterColumn$motoSearchResults$gvCatalog$ctl01$ctl04','')">

<?php
$variable = ob_get_clean();

$startend5 = Array('">' => '</a>');

foreach($startend5 as $o => $p){
   $data = scrape_between($variable, $o, $p);
}
$data = strip_tags($data);
echo $data;
?>

仅供参考 ob_start(); 和 ob_get_clean(); 仅用于示例不想使代码库超过必要的时间,包括所有 curl 命令。

4

1 回答 1

0

我可以建议PHP DOM库吗?您可以像这样访问这些值:

<?php
    include 'simple_html_dom.php';
    $html = str_get_html("<span class=\"current\">1</span><a href=\"javascript:__doPostBack('ctl00\$phCenterColumn\$motoSearchResults\$gvCatalog\$ctl01\$ctl03','')\">2</a><a href=\"javascript:__doPostBack('ctl00\$phCenterColumn\$motoSearchResults\$gvCatalog\$ctl01\$ctl04','')\">");

    $currentPage = $html->find('span.current');
    foreach($currentPage as $page)
    {
        echo 'current page: ' . $page->plaintext . '<br />';
    }

    $otherPages = $html->find('a');
    echo 'other pages: ';
    foreach($otherPages as $otherPage)
    {
        echo $otherPage->plaintext . ' ';
    }
?>

这给了我:

current page: 1
other pages: 2 
于 2013-07-22T23:28:43.927 回答