首页  >  文章  >  后端开发  >  求一段php抓取题目和超链接的代码

求一段php抓取题目和超链接的代码

WBOY
WBOY原创
2016-06-13 12:53:03838浏览

求一段php抓取标题和超链接的代码
比如说http://xcb.nuist.edu.cn/e/wap/list.php?classid=6&style=0&bclassid=1
页面上的新闻"标题"+"时间"+"超链接"



小弟不胜感激,希望直接可以用,网上的我有点用不了,本人没有php基础,还望理解


如果有朋友使用正则表达式来做的话,

  • ].+>(.+)(.+)

  • 这个希望能有点帮助
    php 正则表达式 抓取数据
    ------解决方案--------------------
    $s=file_get_contents('http://xcb.nuist.edu.cn/e/wap/list.php?classid=6&style=0&bclassid=1');<br />
    preg_match_all('/<li><a\s+href="(.+)"[^>]*>(.+)<span>(.+)<\/span><\/a><\/li>/isU',$s,$m);<br />
    print_r($m);

    ------解决方案--------------------
    function func_globalscanlink($strUrl, &$arrAhef, &$arrLink, &$arrTitle, &$strLinkAll)
    {
    $strText = func_ToUtf8(func_ReadPage($strUrl));
    $strText = func_WebFillup($strUrl, $strText);
    if(!preg_match_all("/(]*href[ ]*=[ ]*\"([^]*?)\"[^]*>(.*?))/si", $strText, $arr2A_mat))
    return 0;

    $strLinkAllTem = "";
    for($i = 0; $i  {
    $strLinkTem = $arr2A_mat[2][$i];
    if(strlen($strLinkTem)  continue;
    if(!strpos(" ".$strLinkAllTem, $strLinkTem) && strpos(" ".$strLinkTem, "http://"))
    {
    $strTitleTem = $arr2A_mat[3][$i];
    $strTitleTem = preg_replace("/<.>/si", "", $strTitleTem);
    if(strlen($strTitleTem) > 6)
    {
    $arrAhef[count($arrAhef)] = $arr2A_mat[1][$i];
    $arrLink[count($arrLink)] = $strLinkTem;

    $strTitle = $arr2A_mat[3][$i];
    if(preg_match("/TITLE=\"(.*?)\"/si", $strTitle, $arrTitle_mat))
    $strTitle = $arrTitle_mat[1];
    $arrTitle[count($arrTitle)] = $strTitle;

    $strLinkAll = $strLinkAll.$strLinkTem."\r\n";

    $strLinkAllTem = $strLinkAllTem.$arr2A_mat[2][$i]."\r\n";
    }
    }
    // $strLinkAllTem = $strLinkAllTem.$arr2A_mat[2][$i]."\r\n";
    }

    return $strText;
    }

    func_globalscanlink("http://www.baidu.com/", $arrAhef, $arrLink, $arrTitle, $strLinkAll); //ioooo

    这个函数可以把所有链接和标题都扫出来
    声明:
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn