php屏幕抓取,关于屏幕抓取:如何在PHP中实现Web scraper?

哪些内置的PHP函数对Web抓取有用?有什么好的资源(web或print)可以提高PHP的web抓取速度?

我想推荐我最近遇到的这门课。简单HTML DOM分析器

对于这一点,PHP是一种特别糟糕的语言。它缺少一个事件驱动的框架,这对于这个任务几乎是必要的。你能用它爬一个地方吗?是的。你会爬很多网站吗?不。

@Evancarroll将curl和domdocument适用于从多个网站获取产品的价格和图像(输出到我的网站上)?例如,这个stackoverflow链接如果没有,您会建议什么?

试试看,如果管用的话,对你来说就足够了。节点是一个更好的选择,为建立一个网络刮刀。另外,phantom.js(如果您需要一个真正拥有DOM并在其上运行JavaScript的现代系统)。

刮削一般包括3个步骤:

首先你得到或发布你的请求到指定的URL

下一次你收到返回的HTML响应

最后你分析出你想要的文本刮擦。

为了完成步骤1和2,下面是一个简单的PHP类,它使用curl来获取使用get或post的网页。在返回HTML之后,您只需使用正则表达式通过解析出您想要获取的文本来完成步骤3。

对于正则表达式,我最喜欢的教程站点如下:正则表达式教程

我最喜欢使用regex的程序是regex buddy。我建议你试试那个产品的演示,即使你不想买它。它是一个非常宝贵的工具,甚至可以为您选择的语言(包括PHP)中的regex生成代码。

用途:

$curl = new Curl();

$html = $curl->get("http://www.google.com");

// now, do your regex work against $html

代码>

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);

}

}

?>

嗯,用regex解析HTML是…好吧,我让这家伙解释一下:stackoverflow.com/questions/1732348/…

curl_setopt($this->curl,CURLOPT_COOKIEJAR, $this->cookieJar); curl_setopt($this->curl,CURLOPT_COOKIEFILE, $this->cookieJar);

另外,如果一个人需要从同一个网站上获取或发布多个表单,那么$this->curl = curl_init($url);会出现问题,每次都会打开一个新的会话。此init用于get函数和postform函数

优秀的代码。但库克贾尔和库克叶尔的错误是,用爱德华1〔3〕替换了爱德华1〔2〕。

我推荐Goutte,一个简单的PHP网页刮刀。示例用法:

创建一个Goutte客户端实例(扩展Symfony\Component\BrowserKit\Client:

use Goutte\Client;

$client = new Client();

用request()方法提出要求:

$crawler = $client->request('GET', 'http://www.symfony-project.org/');

request方法返回Crawler对象(Symfony\Component\DomCrawler\Crawler号)。

点击链接:

$link = $crawler->selectLink('Plugins')->link();

$crawler = $client->click($link);

提交表单:

$form = $crawler->selectButton('sign in')->form();

$crawler = $client->submit($form, array('signin[username]' => 'fabien', 'signin[password]' => 'xxxxxx'));

提取数据:

$nodes = $crawler->filter('.error_list');

if ($nodes->count())

{

die(sprintf("Authentification error: %s

", $nodes->text()));

}

printf("Nb tasks: %d

", $crawler->filter('#nb_tasks')->text());

Scraperwiki是一个非常有趣的项目。帮助您在python、ruby或php中在线构建scraper——几分钟后我就可以进行一次简单的尝试了。

如果您需要易于维护而不是快速执行的东西,那么使用可编写脚本的浏览器(如SimpleTest)可能会有所帮助。

刮削可能相当复杂,这取决于您想要做什么。阅读本系列教程,了解用PHP编写scraper的基础知识,看看您是否能够掌握它。

您可以使用类似的方法来自动进行表单注册、登录,甚至是假点击广告!不过,使用curl的主要限制是它不支持使用javascript,因此,如果您试图抓取一个使用Ajax进行分页的站点,例如,它可能会变得有点棘手……但还有一些方法可以解决这个问题!

这里还有一个:一个简单的没有regex的php scraper。

我的框架中的scraper类:

/*

Example:

$site = $this->load->cls('scraper', 'http://www.anysite.com');

$excss = $site->getExternalCSS();

$incss = $site->getInternalCSS();

$ids = $site->getIds();

$classes = $site->getClasses();

$spans = $site->getSpans();

print '[cc lang="php"]';

print_r($excss);

print_r($incss);

print_r($ids);

print_r($classes);

print_r($spans);

*/

class scraper

{

private $url = '';

public function __construct($url)

{

$this->url = file_get_contents("$url");

}

public function getInternalCSS()

{

$tmp = preg_match_all('/(style=")(.*?)(")/is', $this->url, $patterns);

$result = array();

array_push($result, $patterns[2]);

array_push($result, count($patterns[2]));

return $result;

}

public function getExternalCSS()

{

$tmp = preg_match_all('/(href=")(\w.*\.css)"/i', $this->url, $patterns);

$result = array();

array_push($result, $patterns[2]);

array_push($result, count($patterns[2]));

return $result;

}

public function getIds()

{

$tmp = preg_match_all('/(id="(\w*)")/is', $this->url, $patterns);

$result = array();

array_push($result, $patterns[2]);

array_push($result, count($patterns[2]));

return $result;

}

public function getClasses()

{

$tmp = preg_match_all('/(class="(\w*)")/is', $this->url, $patterns);

$result = array();

array_push($result, $patterns[2]);

array_push($result, count($patterns[2]));

return $result;

}

public function getSpans(){

$tmp = preg_match_all('/()(.*)()/', $this->url, $patterns);

$result = array();

array_push($result, $patterns[2]);

array_push($result, count($patterns[2]));

return $result;

}

}

?>

我要么使用libcurl,要么使用perl的lwp(libwww表示perl)。有PHP的libwww吗?

如果要使用lwp,请使用www::mechanical,它用方便的助手函数将其包装起来。

如果您对PHP以外的东西开放的话,Ruby也可以使用机械化。

file_get_contents()可以获取远程URL并提供源代码。然后,您可以使用正则表达式(使用与Perl兼容的函数)来获取所需的内容。

出于好奇,你想刮什么?

curl库允许您下载网页。您应该研究执行刮擦的正则表达式。

-1用于推荐Regex!使用HTML分析器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值