2014年03月20日 星期四 09:39
Goutte是一个非常神奇的PHP Library,他可以模拟浏览器的行为,根据URL获取HTML数据,然后对HTML数据进行分析,可以搜索各种元素(例如button、link等),可以模拟点击链接和提交From的操作,对于经常需要抓取网站数据的同学来说,这个库太有意义了。
官方网站以及使用简洁可以参考:
https://github.com/fabpot/goutte
下面是我写的一个简单示例,用于自动化下载novafile网盘的文件,供大家参考:
<?php
require_once "goutte.phar";
$input_file=$argv[1];
if(empty($input_file)) die("need input file");
$urls=file_get_contents($input_file);
$urls=explode("\n",$urls);
use Goutte\Client;
$client = new Client();
$crawler = $client->request('GET', 'http://novafile.com/login');
$form = $crawler->selectButton('Login')->form();
$crawler = $client->submit($form, array('login' => 'your_username', 'password' => 'your_password'));
foreach($urls as $url) {
try {
$crawler = $client->request('GET', $url);
$form = $crawler->selectButton('Create Download Link')->form();
$crawler = $client->submit($form);
$link = $crawler->selectLink('Download File')->link();
echo $link->getUri();
echo "\n";
} catch (Exception $e ) {
file_put_contents("broken.txt",$url."\n",FILE_APPEND);
}
}
参考资料:
http://api.symfony.com/2.0/Symfony/Component/BrowserKit/Client.html
http://docs.guzzlephp.org/en/latest/
Zeuux © 2025
京ICP备05028076号