package com.heaton.bot;
import com.heaton.bot.*;
import java.net.*;
/**
* The SpiderWorker class performs the actual work of
* spidering pages. It is implemented as a thread
* that is created by the spider class.
*
* Copyright 2001-2003 by Jeff Heaton (http://www.jeffheaton.com)
*
* @author Jeff Heaton
* @version 1.2
*/
public class SpiderWorker extends Thread {
/**
* The URL that this spider worker
* should be downloading.
*/
protected String target;
/**
* The owner of this spider worker class,
* should always be a Spider object.
* This is the class that this spider
* worker will send its data to.
*/
protected Spider owner;
/**
* Indicates if the spider is busy or not.
* true = busy
* false = idle
*/
protected boolean busy;
/**
* A descendant of the HTTP object that
* this class should be using for HTTP
* communication. This is usually the
* HTTPSocket class.
*/
protected HTTP http;
/**
* Constructs a spider worker object.
*
* @param owner The owner of this object, usually
* a Spider object.
* @param http
*/
public SpiderWorker(Spider owner,HTTP http)
{
this.http = http;
this.owner = owner;
}
/**
* Returns true of false to indicate if
* the spider is busy or idle.
*
* @return true = busy
* false = idle
*/
public boolean isBusy()
/SPAN>
return this.busy;
}
/**
* The run method causes this thread to go idle
* and wait for a workload. Once a workload is
* received, the processWorkload method is called
* to handle the workload.
*/
public void run()
{
for ( ;; ) {
target = this.owner.getWorkload();
if ( target==null )
return;
owner.getSpiderDone().workerBegin();
processWorkload();
owner.getSpiderDone().workerEnd();
}
}
/**
* The run method actually performs the
* the workload assigned to this object.
*/
public void processWorkload()
{
try {
busy = true;
Log.log(Log.LOG_LEVEL_NORMAL,"Spidering " + target );
http.send(target,null);
Attribute typeAttribute = http.getServerHeaders().get("Content-Type");
// if no content-type at all, its PROBABLY not HTML
if ( typeAttribute==null )
return;
// now check to see if is HTML, ONLY PARSE text type files(namely HTML)
owner.processPage(http);
if ( !typeAttribute.getValue().startsWith("text/") )
return;
HTMLParser parse = new HTMLParser();
parse.source = new StringBuffer(http.getBody());
// find all the links
while ( !parse.eof() ) {
char ch = parse.get();
if ( ch==0 ) {
HTMLTag tag = parse.getTag();
Attribute link = tag.get("HREF");
if ( link==null )
link = tag.get("SRC");
if ( link==null )
continue;
URL target=null;
E: 7.5pt"> try {
target = new URL(new URL(this.target),link.getValue());
} catch ( MalformedURLException e ) {
Log.log(Log.LOG_LEVEL_TRACE,
"Spider found other link: " + link );
owner.foundOtherLink(link.getValue());
continue;
}
if ( owner.getRemoveQuery() )
target = URLUtility.stripQuery(target);
target = URLUtility.stripAnhcor(target);
if ( target.getHost().equalsIgnoreCase(
new URL(this.target).getHost()) ) {
Log.log(Log.LOG_LEVEL_NORMAL,
"Spider found internal link: " + target.toString() );
owner.foundInternalLink(target.toString());
} else {
Log.log(Log.LOG_LEVEL_NORMAL,
"Spider found external link: " + target.toString() );
owner.foundExternalLink(target.toString());
}
}
}
owner.completePage(http,false);
} catch ( java.io.IOException e ) {
Log.log(Log.LOG_LEVEL_ERROR,
"Error loading file("+ target +"): " + e );
owner.completePage(http,true);
} catch ( Exception e ) {
Log.logException(
"Exception while processing file("+ target +"): ", e );
owner.completePage(http,true);
} finally {
busy = false;
}
}
/**
* Returns the HTTP descendant that this
* object should use for all HTTP communication.
*
* @return An HTTP descendant object.<
/FONT>
*/
public HTTP getHTTP()
{
return http;
}
}
文章出处:http://www.diybl.com/course/3_program/java/javajs/200797/69988_4.html
文章出处:http://www.diybl.com/course/3_program/java/javajs/200797/69988_3.html
文章出处:http://www.diybl.com/course/3_program/java/javajs/200797/69988_2.html
文章出处:http://www.diybl.com/course/3_program/java/javajs/200797/69988.html
分享到:
相关推荐
网络爬虫源代码,word文档,纯代码,开发者参考
python网络爬虫源代码教程以及机器学习 有很多python源代码例程供参考,十分实用
网络爬虫 java代码简单实现。可以供你参考哦。能直接导入工程运行的哦 网络爬虫 java代码简单实现。可以供你参考哦。能直接导入工程运行的哦
c#网络爬虫程序设计C#项目源代码,可供学习及设计参考。
分布式数据库课程大作业-项目名称:分布式网络爬虫+源代码+文档说明+实验报告 - 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分...
基于python的系统开发 应用机器学习、深度学习等技术 源代码 供参考 基于python的系统开发 应用机器学习、深度学习等技术 源代码 供参考 基于python的系统开发 应用机器学习、深度学习等技术 源代码 供参考 基于...
基于ssm框架实现的jd书城网络爬虫 -------- 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! <项目介绍> 1、该资源内项目代码都经过测试运行成功...
在vc下实现网页抓取功能,有一定的参考价值~~~~~~~~~~~
计算机毕业设计Python+Spark游戏推荐系统 游戏可视化 游戏爬虫 神经网络混合CF推荐算法.zip 基于python的系统开发 应用机器学习、深度学习等技术 源代码 供参考 基于python的系统开发 应用机器学习、深度学习等...
基于python的网络爬虫爬取天气数据及可视化分析 python程序设计报告 源代码+csv文件+设计报告 python期末简单大作业(自己写的,重复率低) 利用python爬取了网站上的城市天气,并用利用可视化展示,有参考文献有...
WEBCRAWLER 网络爬虫实训项目 1 WEBCRAWLER 网 络 爬 虫 实 训 项 目 文档版本: 1.0.0.1 编写单位: 达内IT培训集团 C++教学研发部 编写人员: 闵卫 定稿日期: 2015年11月20日 星期五WEBCRAWLER 网络爬虫实训项目 ...
基于Qt的系统项目开发 课程设计 毕业设计 供参考 源代码+说明 基于Qt的系统项目开发 课程设计 毕业设计 供参考 源代码+说明 基于Qt的系统项目开发 课程设计 毕业设计 供参考 源代码+说明 基于Qt的系统项目开发 课程...
这是一个Java网络爬虫(蜘蛛)的源代码文件包,名为"zhizhu.rar"。这个源码文件包是一个非常适合作为毕业设计或课程设计项目的资源。它包含了一个完整的Java网络爬虫程序,可以帮助你学习和理解网络爬虫的原理和实现...
Python网络爬虫与推荐算法的新闻推荐平台 介绍 网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐 权重衰减进行...
它的工作原理是:从网页的url加载网页的源代码之后,会在浏览器里执行JavaScript程序。这些程序会加载更多的内容,“填充”到网页里。这就是为什么如果你直接去爬网页本身的url,你会找不到页面的实际内容。 这里,...
Android 本地网络小说爬虫,基于 jsoup 与 xpath,通过模版解析网页。 - 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分...
此资源集包含了一套完整的Java实现的网络爬虫(蜘蛛)项目源代码、相关的毕业论文以及详尽的使用说明。它旨在提供一个全面、深入的学习和研究工具,适用于本科课程设计、毕业设计以及任何希望深入学习Java编程的学习者...
毕业设计,基于Python+Django+MySql开发的豆瓣电影爬虫采集与分析可视化,内含Python完整源代码,数据库脚本 基于Python豆瓣电影爬虫采集与分析可视化设计 开发环境:Pycharm + Python3.7 + sqlite数据库 + ...
邮箱地址爬虫工具(本地、网络),简单易用,输入本地文件地址或者网址就可以查看所有的邮箱地址,并可保存到本地。工具可参考:http://download.csdn.net/detail/shangguanyunlan/9349699
但是当我们要爬取的页面非常多的时候,单个主机的处理能力就不能满足我们的需求了(无论是处理速度还是网络请求的并发数),这时候分布式爬虫的优势就显现出来。 而Scrapy-Redis则是一个基于Redis的Scrapy分布式组件...