php如何将网页上的新闻抓取并生成列 微信开发

2025-03-09 22:38:51
推荐回答(1个)
回答1:

function fetchURL($url) {
set_time_limit(0);
$url_parsed = parse_url($url);
$host = $url_parsed["host"];
$port = $url_parsed["port"];
if ($port==0)
$port = 80;
$path = $url_parsed["path"];
if ($url_parsed["query"] != "")
$path .= "?".$url_parsed["query"];

$out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";

$fp = fsockopen($host, $port, $errno, $errstr, 30);
fwrite($fp, $out);
$body = false;
while (!feof($fp)) {
$s = fgets($fp, 1024);
if ( $body )
$in .= $s;
if ( $s == "\r\n" )
$body = true;
}
fclose($fp);
unset($url,$url_parsed,$host,$port,$path,$out,$fp,$body,$s);
return $in;
}

$nl=fetchURL(要抓去的页面地址);//抓取远程内容"|]+>(.*)|U"
preg_match_all(获取内容的正则表达式,$nl,$title,PREG_PATTERN_ORDER);//进行正规匹配取得自己要的内容

试一试这种,这是我做的采集