如何在PHP中实现网页抓取工具?
哪些内置的PHP函数可用于网络抓取?有什么好的资源(Web或者印刷)可快速掌握PHP的Web抓取功能?
解决方案
回答
curl库允许我们下载网页。我们应该查看进行抓取的正则表达式。
回答
file_get_contents()
可以使用远程URL并提供源。然后,我们可以使用正则表达式(与Perl兼容的函数)来获取所需的内容。
出于好奇,我们要刮擦什么?
回答
这是一个使用cURL
和file_get_contents
进行网络抓取的不错的教程(链接已删除,见下文)。一定还要阅读以下几部分。
(由于恶意软件警告,直接超链接已删除)
http:// www.oooff.com / php-scripts / basic-php-scraped-data-parsing / basic-php-data-parsing.php
回答
我或者使用libcurl或者使用Perl的LWP(perl的libwww)。是否有用于PHP的libwww?
回答
关于此主题,有一本书"网络机器人,蜘蛛和屏幕抓取工具:使用PHP / CURL开发Internet代理指南",请参见此处的评论。
PHP-Architect在Matthew Turland于2007年12月发行的一篇写得很好的文章中对此进行了介绍。
回答
报废通常包括3个步骤:
- 首先,我们将GET或者POST请求发送到指定的URL
- 接下来,我们将收到作为响应返回的html
- 最终,我们将从该html中解析出要抓取的文本。
要完成第1步和第2步,下面是一个简单的php类,该类使用Curl通过GET或者POST来获取网页。取回HTML之后,我们只需使用正则表达式通过解析我们要抓取的文本来完成第3步。
对于正则表达式,我最喜欢的教程站点如下:
正则表达式教程
我最喜欢使用RegExs的程序是Regex Buddy。即使我们无意购买该产品,我也建议我们尝试该产品的演示。这是一个无价的工具,甚至可以为我们使用选择的语言(包括php)制作的正则表达式生成代码。
回答
我想推荐我最近遇到的这门课。
简单的HTML DOM解析器
回答
$curl = new Curl(); $html = $curl->get("http://www.google.com"); // now, do your regex work against $html PHP Class: <?php class Curl { public $cookieJar = ""; public function __construct($cookieJarFile = 'cookies.txt') { $this->cookieJar = $cookieJarFile; } function setup() { $header = array(); $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; $header[] = "Cache-Control: max-age=0"; $header[] = "Connection: keep-alive"; $header[] = "Keep-Alive: 300"; $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; $header[] = "Accept-Language: en-us,en;q=0.5"; $header[] = "Pragma: "; // browsers keep this blank. curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7'); curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header); curl_setopt($this->curl,CURLOPT_COOKIEJAR, $this->cookieJar); curl_setopt($this->curl,CURLOPT_COOKIEFILE, $this->cookieJar); curl_setopt($this->curl,CURLOPT_AUTOREFERER, true); curl_setopt($this->curl,CURLOPT_FOLLOWLOCATION, true); curl_setopt($this->curl,CURLOPT_RETURNTRANSFER, true); } function get($url) { $this->curl = curl_init($url); $this->setup(); return $this->request(); } function getAll($reg,$str) { preg_match_all($reg,$str,$matches); return $matches[1]; } function postForm($url, $fields, $referer='') { $this->curl = curl_init($url); $this->setup(); curl_setopt($this->curl, CURLOPT_URL, $url); curl_setopt($this->curl, CURLOPT_POST, 1); curl_setopt($this->curl, CURLOPT_REFERER, $referer); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields); return $this->request(); } function getInfo($info) { $info = ($info == 'lasturl') ? curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL) : curl_getinfo($this->curl, $info); return $info; } function request() { return curl_exec($this->curl); } } ?>
听起来我们可能正在尝试"热链接"而不是抓取,即根据其网站内容实时更新?
本教程非常好:
http://www.merchantos.com/makebeta/php/scraping-links-with-php/
我们可能还想看看Prowser。
回答
从我的框架中刮板类:
I'm actually looking to scrape BibleGateway.com as they don't provide an API to access verses for a web app I'm looking to create.
回答
这是另一个:没有Regex的简单PHP Scraper。
回答
ScraperWiki是一个非常有趣的项目。
使用Python,Ruby或者PHP在线构建刮板,我在几分钟内就可以进行一次简单的尝试。
标题数量不匹配
代码数量不匹配