zoukankan      html  css  js  c++  java
  • PHP 获取网页所有连接 PHP

    function getHtml($url, $charset='utf-8')
    {
    	$curl = curl_init();
    	//curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:192.168.168.1', 'CLIENT-IP:192.168.168.1'));//IP
    	curl_setopt($curl, CURLOPT_URL, $url);
    	curl_setopt($curl, CURLOPT_REFERER, "");   //来路
    
    	$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
    
    	curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
    	// 只需返回HTTP header
    	// curl_setopt($curl, CURLOPT_HEADER, 1);
    	// 页面内容我们并不需要
    	// curl_setopt($curl, CURLOPT_NOBODY, 1);
    	// 返回结果,而不是输出它
    	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    	$html = curl_exec($curl);
    	//$info = curl_getinfo($curl);
    
    	//echo var_dump($info);
    
    	if ($html === false) {
    		//echo "cURL Error: " . curl_error($ch);
    
    		return '';
    	}
    
    	curl_close($curl);
    
    	if ($charset != 'utf-8')
    	{
    		$html = iconv($charset, "UTF-8", $html);
    	}
    
    	return $html;
    }
    
    header("Content-type: text/html; charset=utf-8");
    include('simple_html_dom.php');
    
    // 要打开 extension=php_mbstring.dll
    
    //$url = 'http://www.baidu.com/s?wd=kaka';
    $url = 'http://www.163.com/';
    
    $str_html = getHtml($url, 'gbk');
    
    $html = str_get_html($str_html);
    
    $links = $html->find('a');
    
    foreach($links as $link)
    {
    	$txt = trim($link->plaintext);
    	echo $link->href . '[' . $txt . ']<br>';
    }
    
    $html = null;
    

    代码下载:https://files.cnblogs.com/zjfree/php_get_html.rar

  • 相关阅读:
    textarea聚焦的多种写法
    vue.js入门
    全选,反选
    jquery列表,点击反应
    SqlServer中offset..fetch 的使用问题
    复习Spring第四课---Spring对国际化的支持
    SqlServer的order by问题
    设计模式之---代理模式
    解决SpringMVC重复提交的问题
    Java知识复习(三)
  • 原文地址:https://www.cnblogs.com/zjfree/p/3014082.html
Copyright © 2011-2022 走看看