优势:类似jquery的强大搜索DOM的能力。
pq()是一个功能强大的搜索DOM的方法,跟jQuery的$()如出一辙,jQuery的选择器基本上都能使用在phpQuery上,只要把“.”变成“->”,Demo如下(对应我的github的Demo5)
require('phpQuery/phpQuery.php');
phpQuery::newDocumentFile('http://www.baidu.com/');
$menu_a = pq("a");
foreach($menu_a as $a){
echo pq($a)->html()."
";
}
foreach($menu_a as $a){
echo pq($a)->attr("href")."
";
}
?>
优势:过滤能力比较强。
官方给的Demo如下(我的github中对应demo4):
include("PHPCrawl/libs/PHPCrawler.class.php");
class MyCrawler extends PHPCrawler
{
function handleDocumentInfo(PHPCrawlerDocumentInfo $PageInfo)
{ // As example we just print out the URL of the document
echo $PageInfo->url."
";
}
}
$crawler = new MyCrawler();
$crawler->setURL("www.baidu.com");
$crawler->addURLFilterRule("#\.(jpg|gif)$# i");
//过滤到含有这些图片格式的URL
$crawler->go();
?>
优势:提交表单,设置代理等
Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单,
demo如下(对应github中的demo3):
include 'Snoopy/Snoopy.class.php';
$snoopy = new Snoopy();
$url = "http://www.baidu.com";
// $snoopy->fetch($url);
// $snoopy->fetchtext($url);//去除HTML标签和其他的无关数据
$snoopy->fetchform($url);//只获取表单
//只返回网页中链接 默认情况下,相对链接将自动补全,转换成完整的URL。
// $snoopy->fetchlinks($url);
var_dump($snoopy->results);
优势:安装配置到数据库
提供了安装配置,能够直接连接mysql数据库,使用也是比较广泛,这里我们暂时不单独介绍。
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
/* Sends an http request to www.example.com
with additional headers shown above */
$fp = fopen('http://www.example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>
$ch=curl_init(); //初始化一个cURL会话
curl_setopt($ch,CURLOPT_URL,$url);//设置需要获取的 URL 地址
// 设置浏览器的特定header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Host: www.baidu.com",
"Connection: keep-alive",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Upgrade-Insecure-Requests: 1",
"DNT:1",
"Accept-Language: zh-CN,zh;q=0.8,en-GB;q=0.6,en;q=0.4,en-US;q=0.2",
"Cookie:_za=4540d427-eee1-435a-a533-66ecd8676d7d;"
));
$result=curl_exec($ch);//执行一个cURL会话
我们的一个例子
form-demo.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>form-demotitle>
head>
<body>
<form action="./form-demo.php" method="post">
用户名:<input type="text" name="userName"><br>
密 码:<input type="password" name="password"><br>
<input type="submit">
form>
body>
html>
form-demo.php
$userName = $_POST['userName'];
$password = $_POST['password'];
if($userName==="admin"&&$password==="admin"){
echo "hello admin";
}else{
echo "login error";
}
?>
```
提交表单
```php
include 'Snoopy/Snoopy.class.php';
$snoopy = new Snoopy();
$formvars["userName"] = "admin";
//userName 与服务器端/表单的name属性一致
$formvars["password"] = "admin";
$action = "http://localhost:8000/spider/demo3/form-demo.php";//表单提交地址
$snoopy->submit($action,$formvars);
echo $snoopy->results;
?>
class="se-preview-section-delimiter">div>
- 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
问题1:openssl extension required for HTTPS 增加对https的支持
php.in ==> ;extension=php_openssl.dll 去除注释
<div class="se-preview-section-delimiter">div>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
问题2:405 Not Allowed增加
$snoopy->agent = "(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98)"; //伪装浏览器
$snoopy->referer = "http://www.icultivator.com"; //伪装来源页地址 http_referer
$snoopy->rawheaders["Pragma"] = "no-cache"; //cache 的http头信息
$snoopy->rawheaders["X_FORWARDED_FOR"] = "122.0.74.166"; //伪装ip
class="se-preview-section-delimiter">div>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
问题3 : snoopy使用代理
$snoopy->proxy_host = "http://www.icultivator.com";
// HTTPS connections over proxy are currently not supported
$snoopy->proxy_port = "8080"; //使用代理
$snoopy->maxredirs = 2; //重定向次数
$snoopy->expandlinks = true; //是否补全链接 在采集的时候经常用到
$snoopy->maxframes = 5; //允许的最大框架数
- 1
- 2
- 3
- 4
- 5
- 6
问题:
其实尝试了网站进行提交表单是有问题的。这样简单的处理是不能提交表单的,使用代理也是有问题
的。snoopy框架确实会有很多问题,后面有解决思路了再说。
https://blog.csdn.net/future_todo/article/details/52804440