Table of Contents
snoopy.class.php php模拟登陆
Home Backend Development PHP Tutorial PHP采撷利器snoopy应用详解

PHP采撷利器snoopy应用详解

Jun 13, 2016 am 10:58 AM
gt print snoopy submit

PHP采集利器snoopy应用详解

Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单。Snoopy正确运行需要你的服务器的PHP版本在4以上,并且支持PCRE(Perl Compatible Regular Expressions),基本的LAMP服务都支持。

一、Snoopy的一些特点:

1.抓取网页的内容 fetch

2.抓取网页的文本内容 (去除HTML标签) fetchtext

3.抓取网页的链接,表单 fetchlinks fetchform

4.支持代理主机

5.支持基本的用户名/密码验证

6.支持设置user_agent, referer(来路), cookies和header content(头文件)

7.支持浏览器重定向,并能控制重定向深度

8.能把网页中的链接扩展成高质量的url(默认)

9.提交数据并且获取返回值

10.支持跟踪HTML框架

11.支持重定向的时候传递cookies,要求php4以上就可以了,由于本身是php一个类,无需扩支持,服务器不支持curl时候的最好选择。

二、类方法:

fetch($URI)
———–

这是为了抓取网页的内容而使用的方法。$URI参数是被抓取网页的URL地址。抓取的结果被存储在 $this->results 中。如果你正在抓取的是一个框架,Snoopy将会将每个框架追踪后存入数组中,然后存入 $this->results。

fetchtext($URI)
—————

本方法类似于fetch(),唯一不同的就是本方法会去除HTML标签和其他的无关数据,只返回网页中的文字内容。

fetchform($URI)
—————

本方法类似于fetch(),唯一不同的就是本方法会去除HTML标签和其他的无关数据,只返回网页中表单内容(form)。

fetchlinks($URI)
—————-

本方法类似于fetch(),唯一不同的就是本方法会去除HTML标签和其他的无关数据,只返回网页中链接(link)。
默认情况下,相对链接将自动补全,转换成完整的URL。

submit($URI,$formvars)
———————-

本方法向$URL指定的链接地址发送确认表单。$formvars是一个存储表单参数的数组。

submittext($URI,$formvars)
————————–

本方法类似于submit(),唯一不同的就是本方法会去除HTML标签和其他的无关数据,只返回登陆后网页中的文字内容。

submitlinks($URI)
—————-

本方法类似于submit(),唯一不同的就是本方法会去除HTML标签和其他的无关数据,只返回网页中链接(link)。
默认情况下,相对链接将自动补全,转换成完整的URL。

三、类属性: (缺省值在括号里)

$host 连接的主机
$port 连接的端口
$proxy_host 使用的代理主机,如果有的话
$proxy_port 使用的代理主机端口,如果有的话
$agent 用户代理伪装 (Snoopy v0.1)
$referer 来路信息,如果有的话
$cookies cookies, 如果有的话
$rawheaders 其他的头信息, 如果有的话
$maxredirs 最大重定向次数, 0=不允许 (5)
$offsiteok whether or not to allow redirects off-site. (true)
$expandlinks 是否将链接都补全为完整地址 (true)
$user 认证用户名, 如果有的话
$pass 认证用户名, 如果有的话
$accept http 接受类型 (image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*)
$error 哪里报错, 如果有的话
$response_code 从服务器返回的响应代码
$headers 从服务器返回的头信息
$maxlength 最长返回数据长度
$read_timeout 读取操作超时 (requires PHP 4 Beta 4+)
设置为0为没有超时
$timed_out 如果一次读取操作超时了,本属性返回 true (requires PHP 4 Beta 4+)
$maxframes 允许追踪的框架最大数量
$status 抓取的http的状态
$temp_dir 网页服务器能够写入的临时文件目录 (/tmp)
$curl_path cURL binary 的目录, 如果没有cURL binary就设置为 false

四、以下是demo

include “Snoopy.class.php”;
$snoopy = new Snoopy;

$snoopy->proxy_host = “www.baidu.com“;
$snoopy->proxy_port = “8080″;

$snoopy->agent = “(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98)”;
$snoopy->referer = “http://www.baidu.com/“;

$snoopy->cookies["SessionID"] = 238472834723489l;
$snoopy->cookies["favoriteColor"] = “RED”;

$snoopy->rawheaders["Pragma"] = “no-cache”;

$snoopy->maxredirs = 2;
$snoopy->offsiteok = false;
$snoopy->expandlinks = false;

$snoopy->user = “joe”;
$snoopy->pass = “bloe”;

if($snoopy->fetchtext(“http://www.baidu.com“))
{
echo ”

”.htmlspecialchars($snoopy->results).” 
Copy after login
/n”;

}

else

echo “error fetching document: “.$snoopy->error.”/n”;

snoopy采集phpchina示例
//采集phpchina
set_time_limit(0);
require_once(“Snoopy.class.php”);
$snoopy=new Snoopy();
//登陆论坛
$submit_url = “http://www.phpchina.com/bbs/logging.php?action=login“;
$submit_vars["loginmode"] = “normal”;
$submit_vars["styleid"] = “1″;
$submit_vars["cookietime"] = “315360000″;
$submit_vars["loginfield"] = “username”;
$submit_vars["username"] = “***”; //你的用户名
$submit_vars["password"] = “*****”; //你的密码
$submit_vars["questionid"] = “0″;
$submit_vars["answer"] = “”;
$submit_vars["loginsubmit"] = “提 交”;
$snoopy->submit($submit_url,$submit_vars);
if ($snoopy->results)
{
//获取连接地址
$snoopy->fetchlinks(“http://www.phpchina.com/bbs“);
$url=array();
$url=$snoopy->results;
//print_r($url);
foreach ($url as $key=>$value)
{
//匹配http://www.phpchina.com/bbs/forumdisplay.php?fid=156&sid=VfcqTR地址即论坛板块地址
if (!preg_match(“/^(http:////www/.phpchina/.com//bbs//forumdisplay/.php/?fid=)[0-9]*&sid=[a-zA-Z]{6}/i”,$value))
{
unset($url[$key]);
}
}
//print_r($url);
//获取到板块数组$url,循环访问,此处获取第一个模块第一页的数据
$i=0;
foreach ($url as $key=>$value)
{
if ($i>=1)
{
//测试限制
break;
}
else
{
//访问该模块,提取帖子的连接地址,正式访问里需要提取帖子分页的数据,然后根据分页数据提取帖子数据
$snoopy=new Snoopy();
$snoopy->fetchlinks($value);
$tie=array();
$tie[$i]=$snoopy->results;
//print_r($tie);
//转换数组
foreach ($tie[$i] as $key=>$value)
{
//匹配http://www.phpchina.com/bbs/viewthread.php?tid=68127&extra=page%3D1&page=1&sid=iBLZfK
if (!preg_match(“/^(http:////www/.phpchina/.com//bbs//viewthread/.php/?tid=)[0-9]*&extra=page/%3D1&page=[0-9]*&sid=[a-zA-Z]{6}/i”,$value))
{
unset($tie[$i][$key]);
}
}
//print_r($tie[$i]);
//归类数组,将同一个帖子不同页面的内容放一个数组里
$left=”;//连接左边公用地址
$j=0;
$page=array();
foreach ($tie[$i] as $key=>$value)
{
$left=substr($value,0,52);
$m=0;
foreach ($tie[$i] as $pkey=>$pvalue)
{
//重组数组
if (substr($pvalue,0,52)==$left)
{
$page[$j][$m]=$pvalue;
$m++;
}
}
$j++;
}
//去除重复项开始
//$page=array_unique($page);只能用于一维数组
$paget[0]=$page[0];
$nums=count($page);
for ($n=1;$n {
$paget[$n]=array_diff($page[$n],$page[$n-1]);
}
//去除多维数组重复值结束
//去除数组空值
unset($page);
$page=array();//重新定义page数组
$page=array_filter($paget);
//print_r($page);
$u=0;
$title=array();
$content=array();
$temp=”;
$tt=array();
foreach ($page as $key=>$value)
{
//外围循环,针对一个帖子
if (is_array($value))
{
foreach ($value as $k1=>$v1)
{
//页内循环,针对一个帖子的N页
$snoopy=new Snoopy();
$snoopy->fetch($v1);
$temp=$snoopy->results;
//读取标题
if (!preg_match_all(“/

(.*) /h2>/i”,$temp,$tt))
{
echo “no title”;
exit;
}
else
{
$title[$u]=$tt[1][1];
}
unset($tt);
//读取内容
if (!preg_match_all(“/
(.*) /div>/i”,$temp,$tt))
{
print_r($tt);
echo “no content1″;
exit;
}
else
{
foreach ($tt[1] as $c=>$c2)
{
$content[$u].=$c2;
}
}
}
}
else
{
//直接取页内容
$snoopy=new Snoopy();
$snoopy->fetch($value);
$temp=$snoopy->results;
//读取标题
if (!preg_match_all(“/

(.*) /h2>/i”,$temp,$tt))
{
echo “no title”;
exit;
}
else
{
$title[$u]=$tt[1][1];
}
unset($tt);
//读取内容
if (!preg_match_all(“/
(.*) /div>/i”,$temp,$tt))
{
echo “no content2″;
exit;
}
else
{
foreach ($tt[1] as $c=>$c2)
{
$content[$u].=$c2;
}
}
}
$u++;
}
print_r($content);
}
$i++;
}
}
else
{
echo “login failed”;
exit;
}
?>Posted in?PHP / MySQL |?Tagged?PHP,?snoopy |?Leave a reply

snoopy.class.php php模拟登陆

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

What are the uses of Snoopy class in php What are the uses of Snoopy class in php Mar 01, 2024 pm 04:59 PM

Common usage: 1. Send GET request; 2. Send POST request; 3. Set request header; 4. Process Cookie; 5. Download file.

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

How to optimize iPad battery life with iPadOS 17.4 How to optimize iPad battery life with iPadOS 17.4 Mar 21, 2024 pm 10:31 PM

How to Optimize iPad Battery Life with iPadOS 17.4 Extending battery life is key to the mobile device experience, and the iPad is a good example. If you feel like your iPad's battery is draining too quickly, don't worry, there are a number of tricks and tweaks in iPadOS 17.4 that can significantly extend the run time of your device. The goal of this in-depth guide is not just to provide information, but to change the way you use your iPad, enhance your overall battery management, and ensure you can rely on your device for longer without having to charge it. By adopting the practices outlined here, you take a step toward more efficient and mindful use of technology that is tailored to your individual needs and usage patterns. Identify major energy consumers

Where is print on the keyboard? Where is print on the keyboard? Jun 19, 2023 am 09:37 AM

The printscreen key is on the arrow keys of the keyboard device, with the words "prtsc sysrq" on it, and is located to the right of f12. If there is no button with the word "prtsc sysrq", you can find "fn" and "insert(prt sc)", click "fn" first, and then click "insert(PRT sc)" to realize the printscreen screenshot function.

See all articles