首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >去掉页面的标记,得到返回的数组或分隔的页码列表。

去掉页面的标记,得到返回的数组或分隔的页码列表。
EN

Stack Overflow用户
提问于 2013-07-22 22:48:51
回答 1查看 504关注 0票数 3

我试着去掉这个页面的标签,这样我就可以得到一个页码的列表。这样我就可以知道我的卷发程序继续爬行页面的最高页码是多少。现在,我可以把标签去掉到一个可以得到一个数字的点,但我不知道如何把每个数字分开,这样我就可以看到最高页码是多少。

我收到的当前返回值是

12

这是我的代码:

代码语言:javascript
复制
<?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;
?>

FYI -- ob_start();和ob_get_clean();只是在本例中不想使代码库超出必要的时间,包括所有的curl命令。

EN

回答 1

Stack Overflow用户

发布于 2013-07-22 23:28:43

我可以推荐PHP DOM图书馆吗?您可以像这样访问这些值:

代码语言:javascript
复制
<?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 . ' ';
    }
?>

这给了我:

代码语言:javascript
复制
current page: 1
other pages: 2 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17798751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档