5 changed files with 56 additions and 0 deletions
@ -0,0 +1,15 @@ |
|||
package com.example.datacollect; |
|||
|
|||
// 先定义爬虫策略接口(必须先建这个,否则ASiteCrawlStrategy会报错)
|
|||
public interface ASiteCrawlStrategy { |
|||
void crawl(String url); |
|||
} |
|||
|
|||
// A网站爬虫策略类(实现上面的接口)
|
|||
class ASiteCrawlStrategyImpl implements CrawlStrategy { |
|||
@Override |
|||
public void crawl(String url) { |
|||
// 先写简单逻辑:打印爬取信息
|
|||
System.out.println("正在爬取A网站:" + url); |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
package com.example.datacollect; |
|||
|
|||
// 根异常:继承RuntimeException(Unchecked异常),爬虫项目所有异常都继承它
|
|||
public class CrawlException extends RuntimeException { |
|||
// 构造器1:只传错误信息
|
|||
public CrawlException(String message) { |
|||
super(message); |
|||
} |
|||
|
|||
// 构造器2:传错误信息+原始异常(方便排查根因)
|
|||
public CrawlException(String message, Throwable cause) { |
|||
super(message, cause); |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
package com.example.datacollect; |
|||
|
|||
// 网络异常:继承根异常CrawlException
|
|||
public class NetworkException extends CrawlException { |
|||
public NetworkException(String message) { |
|||
// 给错误信息加前缀,方便定位
|
|||
super("网络请求失败:" + message); |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
package com.example.datacollect; |
|||
|
|||
// 解析异常:继承根异常CrawlException
|
|||
public class ParseException extends CrawlException { |
|||
public ParseException(String message) { |
|||
super("数据解析失败:" + message); |
|||
} |
|||
} |
|||
|
|||
|
|||
@ -0,0 +1,8 @@ |
|||
package com.example.datacollect; |
|||
|
|||
// 不支持的网站异常:继承根异常CrawlException
|
|||
public class UnsupportedSiteException extends CrawlException { |
|||
public UnsupportedSiteException(String siteKey) { |
|||
super("不支持的网站类型:" + siteKey); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue