本帖最后由 u014203582 于 2014-05-07 16:54:11 编辑

解决方案 »

  1.   

    和你获取title是 一样的写一个正则就可以了  
    如果是获取一个样式页面 可以根据页面的标签 去获取可以参考 写的非常简单  获取到的是一个数组  学习正则中
      

  2.   


    if (preg_match_all('/href="(.*)"/isU', $html, $match)) {
        print_r($match);
    }
    //匹配到的是 $match[1];自己看数组吧,当然你也可以把匹配条件弄得更详细点
      

  3.   

    我这样写不行的  preg_match_all('|black" href="(.*?)"|i',$info,$m);
      

  4.   

    preg_match_all('|black" href="(.*?)"|is',$info,$m);
      

  5.   

    Array ( [0] => Array ( ) [1] => Array ( ) ) 不行啊  没有得到
      

  6.   

    你的代码是什么?
    $info=<<< TXT
    <span id="dnn_ctr19997_ContentPane" class="DNNAlignleft">
    <span class="Normal">
    <div style="width:490px;padding-top:15px;padding-left:15px;padding-bottom:5px;">
    <div id="dnn_ctr19997_ModuleContent">
    <span id="dnn_ctr19997_ArticleList_ctl00_lblMessage"></span>
    <table id="dnn_ctr19997_ArticleList_ctl00_lstArticles" class="ArticleList" border="0" cellspacing="0" cellpadding="0" style="width:100%;border-collapse:collapse;">
    <tbody>
    <tr>
    <td>
    <table width="100%">
    <tbody>
    <tr>
    <td>
    <a id="dnn_ctr19997_ArticleList_ctl00_lstArticles_ctl00_titleLink" class="viewsblack" href="/Default.aspx?tabid=9597&ctl=Details&mid=19997&ItemID=128217&SkinSrc=[L]Skins/shangshida1_5/shangshida1_5&language=zh-CN" title="“甲子人文底蕴•美丽师大校园”摄影视频作品征集活动通知">“甲子人文底蕴•美丽师大校园”摄影视频作品征集活动...</a>
    </td>
    <td width="136px" valign="top" align="left">
    </tr>
    </tbody>
    </table>
    <div>
    </td>
    </tr>
    TXT;
    preg_match_all('|black" href="(.*?)"|is',$info,$m);
    print_r($m);
    Array
    (
        [0] => Array
            (
                [0] => black" href="/Default.aspx?tabid=9597&ctl=Details&mid=19997&ItemID=128217&SkinSrc=[L]Skins/shangshida1_5/shangshida1_5&language=zh-CN"
            )    [1] => Array
            (
                [0] => /Default.aspx?tabid=9597&ctl=Details&mid=19997&ItemID=128217&SkinSrc=[L]Skins/shangshida1_5/shangshida1_5&language=zh-CN
            ))
      

  7.   

    www.shnu.edu.cn这个是主页  我用你的代码
    结果就是Array ( [0] => Array ( ) [1] => Array ( ) ) 
      

  8.   

    <?php
    $url = 'http://www.shnu.edu.cn/IndexPage.html';  //这儿填页面地址
    $info=file_get_contents($url);
    //$getcontent = iconv("gb2312", "utf-8",$info); 
    preg_match_all('|black" href="(.*?)"|is',$info,$m);
    print_r($m);
    ?>
      

  9.   

    打算获取哪些链接的href? 这样写: preg_match_all('|black".+?href="(.*?)"|is',$info,$m);   可以获取所有满足条件的href。
      

  10.   

        preg_match_all('|class="viewsblack".+?href="(.*?)"|is',$info,$m);
    print_r($m[1]);
      

  11.   

    这样直接截取到的编码应该不是字符类型的吧
    因为'Url' => "http://www.shnu.edu.cn".$n[1][0].""
    这样输出整个网页 点击这个链接 还只是转到主页 并没有跳转到相应的新闻页面
      

  12.   

    <?php 
    function _xinwen()
    {
    $url = 'http://www.shnu.edu.cn/IndexPage.html';  //这儿填页面地址
    $info=file_get_contents($url);
    header("Content-type: text/html; charset=utf-8");
        $getcontent = iconv("gb2312", "utf-8",$info); 
    preg_match_all('|black" title="(.*?)"|i',$info,$m);
        preg_match_all('|class="viewsblack".+?href="(.*?)"|is',$info,$m);
    $news=array();
        $news[] = array('Title' => "师大要闻                                      >>>>>>>>>>>>>>>点击进入首页", 'Description' =>'', 'PicUrl' => 'http://1.shnuzs.sinaapp.com/images/newsPic2.jpg', 'Url' => "http://xw.shnu.edu.cn/");
        for ($i = 0; $i <= 7; $i++) {
           $news[] = array(
    'Title' => $m[1][$i],
    'Description' => '',
              'PicUrl' => 'http://1.shnuzs.sinaapp.com/images/shnu.bmp',
    'Url' => "http://www.shnu.edu.cn".$n[1][$i].""
    );
        }
       return $news;
    }
    ?>
      

  13.   

     preg_match_all('|class="viewsblack".+?href="(.*?)"|is',$info,$n);
      

  14.   

    这个我源码上写的是对的 
    主要的问题是    'Url' => "http://www.shnu.edu.cn".$n[1][$i].""
    这样网址不能连接在一块
    每次点击后跳转的页面 只有 . 前面的
      

  15.   

    为什么不能连在一块?
    print_r($n); 有不有值。你把生成的链接的源码贴出来看看。
      

  16.   

    1、$getcontent = iconv("gb2312", "utf-8",$info);
    这句没有作用,因为下面的处理中仍然是用的是 $info2、
    preg_match_all('|black" title="(.*?)"|i',$info,$m);
    preg_match_all('|class="viewsblack".+?href="(.*?)"|is',$info,$m);
    后面的 $m 把前面的 $m 覆盖了,所以后续代码中不可能得到正确的结果    $url = 'http://www.shnu.edu.cn/IndexPage.html';  //这儿填页面地址
        $info=file_get_contents($url);
        header("Content-type: text/html; charset=utf-8");
        preg_match_all('|black" title="(.*?)"|i',$info,$m);
        preg_match_all('|class="viewsblack".+?href="(.*?)"|is',$info,$n);
        $news=array();
        $news[] = array('Title' => "师大要闻                                      >>>>>>>>>>>>>>>点击进入首页", 'Description' =>'', 'PicUrl' => 'http://1.shnuzs.sinaapp.com/images/newsPic2.jpg', 'Url' => "http://xw.shnu.edu.cn/");
        for ($i = 0; $i <=7; $i++) {
           $news[] = array(
                    'Title' => $m[1][$i],
                    'Description' => '',
                     'PicUrl' => 'http://1.shnuzs.sinaapp.com/images/shnu.bmp',
                    'Url' => "http://www.shnu.edu.cn".$n[1][$i].""
                );
        }
    print_r($news);Array
    (
        [0] => Array
            (
                [Title] => 师大要闻                                      >>>>>>>>>>>>>>>点击进入首页
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/newsPic2.jpg
                [Url] => http://xw.shnu.edu.cn/
            )    [1] => Array
            (
                [Title] => 在调研思考中成长 中青年干部研修班交流课题调研成果
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132454&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [2] => Array
            (
                [Title] => 我校积极推进奉贤区产学研协同创新工作 先进个人获表彰
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132443&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [3] => Array
            (
                [Title] => 学校召开行政例会部署近期工作
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132442&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [4] => Array
            (
                [Title] => 校领导率队赴上海大学学习考察信息化建设工作
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132441&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [5] => Array
            (
                [Title] => 十篇论文获评学报(自然科学版)首届优秀论文
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132299&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [6] => Array
            (
                [Title] => 我校世界电影研究中心师生入选上海国际电影节选片人
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132331&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [7] => Array
            (
                [Title] => 我校召开2014年上海师范大学教育实习计划协调会
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132326&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [8] => Array
            (
                [Title] => “多元文化•多彩世界”校外语综合能力大赛开幕
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132358&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            ))
      

  17.   

    这样看起来是 网址都连在一块了  而且我是return回去一个数组
    然后我在微信端 是用图文格式 输出的
    标题都是正确的  但点击标题后  进去的只是www.shnu.edu.cn  并没有跳转到完整的地址
      

  18.   

    Array
    (
        [0] => Array
            (
                [Title] => 师大要闻                                      >>>>>>>>>>>>>>>点击进入首页
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/newsPic2.jpg
                [Url] => http://xw.shnu.edu.cn/
            )    [1] => Array
            (
                [Title] => 在调研思考中成长 中青年干部研修班交流课题调研成果
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132454&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [2] => Array
            (
                [Title] => 我校积极推进奉贤区产学研协同创新工作 先进个人获表彰
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132443&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [3] => Array
            (
                [Title] => 学校召开行政例会部署近期工作
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132442&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [4] => Array
            (
                [Title] => 校领导率队赴上海大学学习考察信息化建设工作
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132441&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [5] => Array
            (
                [Title] => 十篇论文获评学报(自然科学版)首届优秀论文
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132299&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [6] => Array
            (
                [Title] => 我校世界电影研究中心师生入选上海国际电影节选片人
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132331&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [7] => Array
            (
                [Title] => 我校召开2014年上海师范大学教育实习计划协调会
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132326&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            )    [8] => Array
            (
                [Title] => “多元文化•多彩世界”校外语综合能力大赛开幕
                [Description] => 
                [PicUrl] => http://1.shnuzs.sinaapp.com/images/shnu.bmp
                [Url] => http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132358&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
            ))
    url打印的时候 都是完整的  
    这样看起来是 网址都连在一块了  而且我是return回去一个数组
    然后我在微信端 是用图文格式 输出的
    标题都是正确的  但点击标题后  进去的只是www.shnu.edu.cn  并没有跳转到完整的地址 
      

  19.   

    可能是 & 被转义成 &amp; 的原因
    你至少要将 Url 像他那样放在引号里
      

  20.   

    'Url' => "http://www.shnu.edu.cn.$n[1][$i]."
    这样吗?
      

  21.   

    你取到的 url 是正确的
    但你在使用这些 url 时可能写的不对
      

  22.   

    就只有这一个地方用了'Url' => "http://www.shnu.edu.cn".$n[1][$i].""
      

  23.   

    你这是获取!
    这个 _xinwen 函数总是有人在调用吧
      

  24.   

    $items = _xinwen();
                         $itemTpl = "<item>
                                        <Title><![CDATA[%s]]></Title>
                                        <Description><![CDATA[%s]]></Description>
                                        <PicUrl><![CDATA[%s]]></PicUrl>
                                        <Url><![CDATA[%s]]></Url>
                                        </item>";
                            $articles = '';
                            foreach ($items as $key)
                            {
                                $articles.=sprintf($itemTpl,$key['Title'],$key['Description'],$key['PicUrl'],$key['Url']);
                            }
                            $newsTpl = "<xml>
                                       <ToUserName><![CDATA[%s]]></ToUserName>
                                       <FromUserName><![CDATA[%s]]></FromUserName>
                                       <CreateTime>%s</CreateTime>
                                       <MsgType><![CDATA[%s]]></MsgType>
                                       <ArticleCount><![CDATA[%s]]></ArticleCount>
                                       <Articles>%s</Articles>
                                       </xml>";
                            echo sprintf($newsTpl,$fromUsername,$toUsername,$time,'news',count($items),$articles);
    xml消息输出
      

  25.   

    怎么不让&转义?
      

  26.   

    这不就对了吗?
    你抓取到的是这样的 url:
    http://www.shnu.edu.cn/Default.aspx?tabid=9597&amp;ctl=Details&amp;mid=19994&amp;ItemID=132331&amp;SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&amp;language=zh-CN
    即字段分隔符 & 是被转义的
    虽然这样的 url 直接使用并没问题,但如果再转义一次就不行了!
    所以你应该处理成这样的
    http://www.shnu.edu.cn/Default.aspx?tabid=9597&ctl=Details&mid=19994&ItemID=132331&SkinSrc=[L]Skins/shangshida1_4/shangshida1_4&language=zh-CN
    即把 amp; 删去,就应该没有问题了
      

  27.   

    'Url' => "http://www.shnu.edu.cn".$n[1][$i]
    改成
    'Url' => "http://www.shnu.edu.cn". str_replace('amp;', '', $n[1][$i])