<?php
 class ContentAlbb implements UrlInterface
 {
  private $url="http://search.china.alibaba.com/company/k-%BE%C6%B5%EA_n-y.html";
  private $expression="/\<span class\=\"m undline\"\>\<a href\=\"(.+?)\/\"  onmousedown\=/is";
  public $introduce=array();
  function content()
  {
   $contents = file_get_contents($this->url);
   //echo $contents;
   preg_match_all($this->expression,$contents,$out);
   $url=$out[1][0];
   $urlStr=explode('.',$this->url);
   $urlStrTwo=$urlStr[0];
   $twoUrl=subStr($urlStrTwo,7);
   $lastUrl=$url."/athena/contact/".$twoUrl.".html"."<br>";
   echo $lastUrl;
   $contentsTwo=file_get_contents($lastUrl);
   echo $contentsTwo;
  }
 }?>
为什么echo $contentsTwo;输出不出来呢,抓取的是阿里巴巴的中每个公司的页面 ,应该不是网络问题,页面什么的都可以打开的

解决方案 »

  1.   

    $con=file_get_contents("http://search.china.alibaba.com/company/k-%BE%C6%B5%EA_n-y.html");$preg ="~<span class=\"m undline\"><a href=\"([^\"'>\s]+[^\"'>\s]+)\"([^>]+)>(.*?)</a></span>~is";preg_match_all($preg,$con,$arr);print_r($arr);
      

  2.   

    $con=file_get_contents("http://search.china.alibaba.com/company/k-%BE%C6%B5%EA_n-y.html");$preg ="~<span class=\"m undline\"><a href=\"([^\"'>\s]+[^\"'>\s]+)\"[^>]+>(.*?)</a></span>~is";//这个就没有onmouse那部分了。只有公司地址和名称preg_match_all($preg,$con,$arr);print_r($arr);
      

  3.   

    $lastUrl=$url."/athena/contact/".$twoUrl.".html"."<br>";
    改为:$lastUrl=$url."athena/contact/".$twoUrl.".html";
    汗,多了br就不说了,多了个/,直接在浏览器都访问不到的。
      

  4.   

    不多/ 啊,多个<br> ,有时还出现超时是怎么回事呀??Fatal error: Maximum execution time of 30 seconds exceeded in D:\myphp_prject\URL\ContentAlbb.php on line 10
      

  5.   

    http://rxkjfz.cn.alibaba.com/athena/contact/search.html//可以访问
    http://rxkjfz.cn.alibaba.com//athena/contact/search.html//找不到数据。超时是因为,file_get_contents没有useragent或其他服务器需要的头。
    自己构造个头,再获取。
      

  6.   

    echo $lastUrl;
    输出的是:http://rxkjfz.cn.alibaba.com/athena/contact/search.html/
    但是
    echo $contentsTwo;
    却不显示啊!到底是怎么回事???
    很急的呀!
      

  7.   

    $opts = array(
      'http'=>array(
        'method'=>"GET",
        'header'=>"Host: style.china.alibaba.com\r\n" . 
                  "Accept-language: zh-cn\r\n" . 
                  "User-Agent: mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv:1.9.2.3) gecko/20100401 firefox/3.6.3\r\n" .
                  "Accept: */*"
      )
    );
    //http://rxkjfz.cn.alibaba.com/athena/contact/search.html
    $context = stream_context_create($opts);
    $url = "http://rxkjfz.cn.alibaba.com/athena/contact/rxkjfz.html";
    echo file_get_contents($url, False, $context);
    老田不是都给你说了吗?这里你就把:
    http://rxkjfz.cn.alibaba.com/athena/contact/search.html
    替换为:
    http://rxkjfz.cn.alibaba.com/athena/contact/rxkjfz.html构造URL的时候注意下就可以了。前者不能,就用后者。