1. 程式人生 > >PHP爬蟲最全總結2-phpQuery,PHPcrawer,snoopy框架中文介紹

PHP爬蟲最全總結2-phpQuery,PHPcrawer,snoopy框架中文介紹

prev opts 默認 eee 我們 sso 方法 內容 section

1.幾種常用的PHP爬蟲框架對比

原文鏈接:https://blog.csdn.net/future_todo/article/details/52804440

1.1 phpQuery

優勢:類似jquery的強大搜索DOM的能力。
pq()是一個功能強大的搜索DOM的方法,跟jQuery的$()如出一轍,jQuery的選擇器基本上都能使用在phpQuery上,只要把“.”變成“->”,Demo如下(對應我的github的Demo5)

<?php 
 require(‘phpQuery/phpQuery.php‘);
 phpQuery::newDocumentFile(‘http://www.baidu.com/‘); 
 
$menu_a = pq("a"); foreach($menu_a as $a){ echo pq($a)->html()."<br>"; } foreach($menu_a as $a){ echo pq($a)->attr("href")."<br>"; } ?>

1.2 PHPcrawer

優勢:過濾能力比較強。
官方給的Demo如下(我的github中對應demo4):

<?php 
    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."<br>"; } } $crawler = new MyCrawler(); $crawler->setURL("www.baidu.com"); $crawler
->addURLFilterRule("#\.(jpg|gif)$# i"); //過濾到含有這些圖片格式的URL $crawler->go(); ?>

1.3 snoopy

優勢:提交表單,設置代理等
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);

1.4 phpspider

優勢:安裝配置到數據庫
提供了安裝配置,能夠直接連接mysql數據庫,使用也是比較廣泛,這裏我們暫時不單獨介紹。

2.模擬用戶行為

2.1 file_get_contents

<?php
$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);
?>

2.2 curl

$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會話

2.3 snoopy

  • 表單提交

我們的一個例子
form-demo.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>form-demo</title>
</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

<?php 
    $userName = $_POST[‘userName‘];
    $password = $_POST[‘password‘];
    if($userName==="admin"&&$password==="admin"){
        echo "hello admin";
    }else{
        echo "login error";
    }
 ?>
 ```
提交表單
```php
<?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;
?>




<div class="se-preview-section-delimiter"></div>

PHP爬蟲最全總結2-phpQuery,PHPcrawer,snoopy框架中文介紹