一文详解使用PHP编写爬虫的方法

藏色散人
发布: 2023-04-10 11:26:01
转载
7615 人浏览过

说到爬虫,大家的第一印象就会想到Python, 但是Python并不是所有人都会的, 那么是否可以使用其他的语言来编写爬虫呢? 当然是可以的,下面介绍一下如何使用PHP编写爬虫。

获取页面html内容

1、使用函数 file_get_contents 把整个文件读入一个字符串中。

file_get_contents(path,include_path,context,start,max_length); file_get_contents('https://fengkui.net/');
登录后复制

这样就可以将整个页面的html内容,读入一个字符串中,然后进行解析了

2、使用CURL进行请求,获取html

/** * [curlHtml 获取页面信息] * @param [type] $url [网址] * @return [type] [description] */ function curlHtml($url) { $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "{$url}", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => array( "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding: gzip, deflate, br", "Accept-Language: zh-CN,zh;q=0.9", "Cache-Control: no-cache", "Connection: keep-alive", "Pragma: no-cache", "Upgrade-Insecure-Requests: 1", "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36", "cache-control: no-cache" ), )); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) return false; else return $response; }
登录后复制

使用Curl我们可以来进行其他操作,如模拟浏览器模拟登陆等一些高级操作。

解析页面HTML,获取所需数据

1、正则获取内容

/** * [get_tag_data 使用正则获取html内容] * @param [type] $html [爬取的页面内容] * @param [type] $tag [要查找的标签] * @param [type] $attr [要查找的属性名] * @param [type] $value [属性名对应的值] * @return [type] [description] */ function get_tag_data($html,$tag,$attr,$value){ $regex = "/<$tag.*?$attr=\".*?$value.*?\".*?>(.*?)<\/$tag>/is"; preg_match_all($regex,$html,$matches,PREG_PATTERN_ORDER); $data = isset($matches[1][0]) ? $matches[1][0] : ''; return $data; } $str = '
冯奎博客
'; $value = get_tag_data($str, 'div', 'class', 'feng');
登录后复制

2、使用Xpath解析数据

XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言。 具体使用方法及相关介绍查看百度百科(XPath) 使用方法:

/** * [get_html_data 使用xpath对获取到的html内容进行处理] * @param [type] $html [爬取的页面内容] * @param [type] $path [Xpath语句] * @param integer $tag [类型 0内容 1标签内容 自定义标签] * @param boolean $type [单个 还是多个(默认单个时输出单个)] * @return [type] [description] */ function get_html_data($html,$path,$tag=1,$type=true) { $dom = new \DOMDocument(); @$dom->loadHTML("" . $html); // 从一个字符串加载HTML并设置UTF8编码 $dom->normalize(); // 使该HTML规范化 $xpath = new \DOMXPath($dom); //用DOMXpath加载DOM,用于查询 $contents = $xpath->query($path); // 获取所有内容 $data = []; foreach ($contents as $value) { if ($tag==1) { $data[] = $value->nodeValue; // 获取不带标签内容 } elseif ($tag==2) { $data[] = $dom->saveHtml($value); // 获取带标签内容 } else { $data[] = $value->attributes->getNamedItem($tag)->nodeValue; // 获取attr内容 } } if (count($data)==1) { $data = $data[0]; } return $data; }
登录后复制

推荐学习:《PHP视频教程

以上是一文详解使用PHP编写爬虫的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:fengkui.net
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!