2019年我接触到PHP爬虫的时候,我最开始是懵的。还有人用php来写爬虫?
一个月之后,嗯~全世界最好的语言写全世界最好的爬虫,真香!而在7月15这一个日常加班的晚上,做完手头的活,我寻思着写会儿php就撤,写完一看才九点,这么早下班弟弟我配吗?于是,这篇博客出炉了!

简单说下我使用PHP爬取web数据常用的三种方法,不仅是分享,也是自己的一次复习吧。
希望对你有所启发与帮助 :)。print_r(“源码在文末”);

1.PHP file_get_contents()

file_get_content()函数早在PHP4便出现了,该函数可以把整个文件读入一个字符串中。使用语法为file_get_contents(path,include_path,context,start,max_length);
这里放个截图:

那怎么用这个函数来爬取网页的数据呢?如果这个文件是在线上的,能把这个文件读进一个str里吗,答案是可以。所以只要简单的一句就能搞定网页源码:(当然这里是不经过js渲染的哈,爬取网站http://top.baidu.com/?fr=mhd_card)

	function get_content()
	{
		$html_source = file_get_contents($this->url);
		return $html_source;
	}

2.PHP cURL

php的curl应该是我当时用的最多的php爬取方法了。
使用cURL可以处理很多复杂的页面,比如表单提交、FTP上传、同时也支持HTTPS认证
放个链接在这在这想深究的同学可以看看用法。

	function get_curl()
	{
		//初始化
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->url);
		//设置超时
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		// 如果是请求https时,要打开下面两个ssl安全校验
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//表示string输出,0为直接输出;
		curl_setopt($ch,CURLOPT_HEADER,0);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headerArray);
		$html_source = curl_exec($ch);
		curl_close($ch);
		// $result = json_decode($output,true); 请求部分接口会返回json可以用到
		return $html_source;
	}

3.PHP Snoopy.class.php采集类

百度百科这样说:

好!那我们就,下载Snoopy.class.php(留言私发),引入使用即可。

include('Snoopy.class.php');
function getsnoopy()
{
	$snoopy = new Snoopy();
	$snoopy->fetch($this->url);
	$html_source = $snoopy->results;
	return $html_source;
}

当然,这里只是Snoopy一个最简单的用法,抓取源码。
抓取源码之后使用正则,或是xpath,又或是其他提取方法来抓取你想要的数据,这里么就不谈,21:48,想下班了。

//所有title的正则表达式
$parament = "/<a.*?class=\"list-title\".*>.*?<\/a>/";
preg_match_all($parament,$html_source,$matchs);
print_r($matchs);

就写这些吧,下班走人。

4.爬取结果和源码

对!你没看错,我懒得处理数据了…有些事情,结果并不重要对吧~重要的是过程
附上代码:

<?php
/** * @author fang * @todo snoopy.class\curl\file_get_contents采集类的用法 * @createtime 2020/07/15 */
include('Snoopy.class.php');
/** * php爬虫和采集类 */
class Spider 
{
	//初始化url、cookie、header...
	private $url;
	private $headerArray;
	function __construct()
	{
		$this->url = "http://top.baidu.com/?fr=mhd_card";
		$this->headerArray = 
						array("Content-type:application/json;","Accept:application/json");
	}

	//spider1 file_get_content
	function get_content()
	{
		$html_source = file_get_contents($this->url);
		return $html_source;
	}

	//spider2 snoopy
	function getsnoopy()
	{
		$snoopy = new Snoopy();
		$snoopy->fetch($this->url);
		$html_source = $snoopy->results;
		return $html_source;
	}

	//spider3 curl
	function get_curl()
	{
		//初始化
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->url);
		//设置超时
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		// 如果是请求https时,要打开下面两个ssl安全校验
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//表示string输出,0为直接输出;
		curl_setopt($ch,CURLOPT_HEADER,0);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headerArray);
		$html_source = curl_exec($ch);
		curl_close($ch);
		// $result = json_decode($output,true); 请求部分接口会返回json可以用到
		return $html_source;
	}
}

$spider = new Spider();
// $html_source = $spider->get_content();
// $html_source = $spider->getsnoopy();
$html_source = $spider->get_curl();
$parament = "/<a.*?class=\"list-title\".*>.*?<\/a>/";
preg_match_all($parament,$html_source,$matchs);
print_r($matchs);

本文地址:https://blog.csdn.net/fang_mu_mu/article/details/107370412