How to implement a web scraper in PHP?
哪些内置的PHP函数对Web抓取有用?有什么好的资源(web或print)可以提高PHP的web抓取速度?
刮削一般包括3个步骤:
- 首先你得到或发布你的请求到指定的URL
- 下一次你收到返回的HTML响应
- 最后你分析出你想要的文本刮擦。
为了完成步骤1和2,下面是一个简单的PHP类,它使用curl来获取使用get或post的网页。在返回HTML之后,您只需使用正则表达式通过解析出您想要获取的文本来完成步骤3。 对于正则表达式,我最喜欢的教程站点如下:正则表达式教程 我最喜欢使用regex的程序是regex buddy。我建议你试试那个产品的演示,即使你不想买它。它是一个非常宝贵的工具,甚至可以为您选择的语言(包括PHP)中的regex生成代码。 用途:
1 2 3 4 5 6 7 8 9
| </p>
<p>
$curl = new Curl();
$html = $curl->get("http://www.google.com");
</p>
<p>
// now, do your regex work against $html |
代码> PHP类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| <?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);
}
}
?>
</wyn> |
我推荐Goutte,一个简单的PHP网页刮刀。 示例用法: 创建一个Goutte客户端实例(扩展Symfony\Component\BrowserKit\Client:
1 2 3
| use Goutte\Client;
$client = new Client(); |
用request()方法提出要求:
1
| $crawler = $client->request('GET', 'http://www.symfony-project.org/'); |
request方法返回Crawler对象(Symfony\Component\DomCrawler\Crawler号)。 点击链接:
1 2
| $link = $crawler->selectLink('Plugins')->link();
$crawler = $client->click($link); |
提交表单:
1 2
| $form = $crawler->selectButton('sign in')->form();
$crawler = $client->submit($form, array('signin[username]' => 'fabien', 'signin[password]' => 'xxxxxx')); |
提取数据:
1 2 3 4 5 6 7 8 9 10
| $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类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| <?php
/*
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('/(<span>)(.*)(<\/span>)/', $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吗?
file_get_contents()可以获取远程URL并提供源代码。然后,您可以使用正则表达式(使用与Perl兼容的函数)来获取所需的内容。 出于好奇,你想刮什么?
curl库允许您下载网页。您应该研究执行刮擦的正则表达式。
|