<?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;输出不出来呢,抓取的是阿里巴巴的中每个公司的页面 ,应该不是网络问题,页面什么的都可以打开的
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;输出不出来呢,抓取的是阿里巴巴的中每个公司的页面 ,应该不是网络问题,页面什么的都可以打开的
改为:$lastUrl=$url."athena/contact/".$twoUrl.".html";
汗,多了br就不说了,多了个/,直接在浏览器都访问不到的。
http://rxkjfz.cn.alibaba.com//athena/contact/search.html//找不到数据。超时是因为,file_get_contents没有useragent或其他服务器需要的头。
自己构造个头,再获取。
输出的是:http://rxkjfz.cn.alibaba.com/athena/contact/search.html/
但是
echo $contentsTwo;
却不显示啊!到底是怎么回事???
很急的呀!
'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的时候注意下就可以了。前者不能,就用后者。