search
HomeBackend DevelopmentPHP TutorialPHP采撷利器snoopy应用详解

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).” 
/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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment