本文目录一览:
- php获取指定网页内容
- php获取指定网站的文章标题以及连接
- php如何通过地址去获取一个网页的标题title里面的内容
- php网页怎么抓取一部分正文作为标题
- 求一个简易的php爬虫提取网页的title
- php远程读取标题编码问题
php获取指定网页内容
一、用file_get_contents
函数,以post方式获取url
<?php
$url = "";
$data = array('foo' => 'bar');
$data = http_build_query($data);
$opts = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
$ctx = stream_context_create($opts);
$html = @file_get_contents($url, '', $ctx);
二、用file_get_contents
以get方式获取内容
<?php
$url = '';
$html = file_get_contents($url);
echo $html;
?>
三、用fopen
打开url, 以get方式获取内容
<?php
$fp = fopen($url, 'r');
$header = stream_get_meta_data($fp); //获取报头信息
while (!feof($fp)) {
$result .= fgets($fp, 1024);
}
echo "url header: {$header} <br>";
echo "url body: $result";
fclose($fp);
?>
四、用fopen
打开url, 以post方式获取内容
<?php
$data = array('foo2' => 'bar2', 'foo3' => 'bar3');
$data = http_build_query($data);
$opts = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\nCookie:cook1=c3;cook2=c4\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
$context = stream_context_create($opts);
$html = fopen(';id2=i4', 'rb', false, $context);
$w = fread($html, 1024);
echo $w;
?>
五、使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展
<?php
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
php获取指定网站的文章标题以及连接
写一个正则匹配就可以了
<?php
$html = 'li................../li';
preg_match_all('/<a\s+href="(.*)">(.*)<\/a><span\s+style="color:#F00;"(.*)<\/span><\/li>/Ui', $html, $data);
print_r($data); //就有数据了注意空格那些都要和代码里的一致
?>
php如何通过地址去获取一个网页的标题title里面的内容
具体代码如下:
<?php
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
PHP 独特的语法混合了C、Java、Perl以及PHP自创的语法。 它可以比CGI或者Perl更快速地执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML(标准通用标记语言下的一个应用)文档中去执行, 执行效率比完全生成HTML标记的CGI要高许多; PHP还可以执行编译后代码,编译可以达到加密和优化代码运行,使代码运行更快。
php网页怎么抓取一部分正文作为标题
截取 前n个字符不可以吗?
<?php
$config['title'] = mb_substr($config['title'], 0, 20);
?>
求一个简易的php爬虫提取网页的title
<?php
header("Content-Type: text/html; charset=gbk");
$url = "";
$fcontents = file_get_contents($url);
if (ereg("<title>(.*)</title>", $fcontents, $regs)) {
echo "ok";
} else {
echo "error";
}
echo "<br>";
print_r($regs);
?>
php远程读取标题编码问题
没法定义, php不会自动转码,或者http协议里也不会按照你的参数设置自动转码 所以,这需要你自己用代码去转换编码 思路:
- 连接网页读取数据.
- 从header头信息里或者网页代码里获取网页的编码方式(字符集,gbk,utf8等)
- 根据需要把数据转换成你要的字符集
- 解析数据 注:
- 抓数据,若使用
file()
,file_get_contents()
等,网页字符集信息,可从html代码里用正则匹配出来
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- 若使用
fsockopen()
抓数据, 可从http响应头里取的字符集.但响应头里也可能没有字符集信息.最好再结合html头部信息解析下 - php的正则,建议使用
preg
库,那个功能和性能都更好些