WebMagic的结构分为Downloader、PageProcessor、Scheduler、Pipeline四大组件,并由Spider将它们彼此组织起来。这四大组件对应爬虫生命周期中的下载、处理、管理和持久化等功能
PageProcessor 需要自己写
Scheduler 除非项目有一些特殊的分布式需求,否则无需自己定制
Pipeline 要保存到数据库需要自己定制
Selectable
| 方法 | 说明 | 示例 |
| xpath(String xpath) |
使用XPath选择 |
html.xpath("//div[@class='title']") |
| $(String selector) |
使用Css选择器选择 |
html.$("div.title") |
| $(String selector,String attr) |
使用Css选择器选择 |
html.$("div.title","text") |
| css(String selector) |
功能同$(),使用Css选择器选择 |
html.css("div.title") |
| links() |
选择所有链接 |
html.links() |
| regex(String regex) |
使用正则表达式抽取 |
html.regex("<div>(.*?)") |
| regex(String regex,int group) |
使用正则表达式抽取,并指定捕获组 |
html.regex("<div>(.*?)",1) |
| replace(String regex, String replacement) |
替换内容 |
html.replace("","")
|
返回结果
| 方法 | 说明 | 示例 |
| get() |
返回一条String类型的结果 |
String link= html.links().get() |
| toString() |
功能同get(),返回一条String类型的结果 |
String link= html.links().toString() |
| all() |
返回所有抽取结果 |
List links= html.links().all() |
| match() |
是否有匹配结果 |
if (html.links().match()){ xxx; } |
Spider
| 方法 | 说明 | 示例 |
| create(PageProcessor) |
创建Spider |
Spider.create(new GithubRepoProcessor()) |
| addUrl(String…) |
添加初始的URL |
spider .addUrl("http://webmagic.io/docs/") |
| addRequest(Request...) |
添加初始的Request |
spider .addRequest("http://webmagic.io/docs/") |
| thread(n) |
开启n个线程 |
spider.thread(5) |
| run() |
启动,会阻塞当前线程执行 |
spider.run() |
| start()/runAsync() |
异步启动,当前线程继续执行 |
spider.start() |
| stop() |
停止爬虫 |
spider.stop() |
| test(String) |
抓取一个页面进行测试 |
spider .test("http://webmagic.io/docs/") |
| addPipeline(Pipeline) |
添加一个Pipeline,一个Spider可以有多个Pipeline |
spider .addPipeline(new ConsolePipeline()) |
| setScheduler(Scheduler) |
设置Scheduler,一个Spider只能有个一个Scheduler |
spider.setScheduler(new RedisScheduler()) |
| setDownloader(Downloader) |
设置Downloader,一个Spider只能有个一个Downloader |
spider .setDownloader(new SeleniumDownloader()) |
| get(String) |
同步调用,并直接取得结果 |
ResultItems result = spider .get("http://webmagic.io/docs/") |
| getAll(String…) |
同步调用,并直接取得一堆结果 |
List<ResultItems> results = spider .getAll("http://webmagic.io/docs/", "http://webmagic.io/xxx") |
Site
| 方法 | 说明 | 示例 |
| setCharset(String) |
设置编码 |
site.setCharset("utf-8") |
| setUserAgent(String) |
设置UserAgent |
site.setUserAgent("Spider") |
| setTimeOut(int) |
设置超时时间,单位是毫秒 |
site.setTimeOut(3000) |
| setRetryTimes(int) |
设置重试次数 |
site.setRetryTimes(3) |
| setCycleRetryTimes(int) |
设置循环重试次数 |
site.setCycleRetryTimes(3) |
| addCookie(String,String) |
添加一条cookie |
site.addCookie("dotcomt_user","code4craft") |
| setDomain(String) |
设置域名,需设置域名后,addCookie才可生效 |
site.setDomain("github.com") |
| addHeader(String,String) |
添加一条addHeader |
site.addHeader("Referer","https://github.com") |
| setHttpProxy(HttpHost) |
设置Http代理 |
site.setHttpProxy(new HttpHost("127.0.0.1",8080))
|
Xsoup
| Name |
Expression |
Support |
| nodename |
nodename |
yes |
| immediate parent |
/ |
yes |
| parent |
// |
yes |
| attribute |
[@key=value] |
yes |
| nth child |
tag[n] |
yes |
| attribute |
/@key |
yes |
| wildcard in tagname |
/* |
yes |
| wildcard in attribute |
/[@*] |
yes |
| function |
function() |
part |
| or |
a | b |
yes since 0.2.0 |
| parent in path |
. or .. |
no |
| predicates |
price>35 |
no |
| predicates logic |
@class=a or @class=b |
yes since 0.2.0 |
另外作者自己定义了几个对于爬虫来说,很方便的XPath函数。但是请注意,这些函数式标准XPath没有的。
| Expression | Description | XPath1.0 |
| text(n) |
第n个直接文本子节点,为0表示所有 |
text() only |
| allText() |
所有的直接和间接文本子节点 |
not support |
| tidyText() |
所有的直接和间接文本子节点,并将一些标签替换为换行,使纯文本显示更整洁 |
not support |
| html() |
内部html,不包括标签的html本身 |
not support |
| outerHtml() |
内部html,包括标签的html本身 |
not support |
| regex(@attr,expr,group) |
这里@attr和group均可选,默认是group0 |
not support
|
代理
| API | 说明 |
| HttpClientDownloader.setProxyProvider(ProxyProvider proxyProvider) |
设置代理 |
1.设置单一的普通HTTP代理为101.101.101.101的8888端口,并设置密码为"username","password"
HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
httpClientDownloader.setProxyProvider(SimpleProxyProvider.from(new Proxy("101.101.101.101",8888,"username","password")));
spider.setDownloader(httpClientDownloader);
2.设置代理池,其中包括101.101.101.101和102.102.102.102两个IP,没有密码
HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
httpClientDownloader.setProxyProvider(SimpleProxyProvider.from(
new Proxy("101.101.101.101",8888)
,new Proxy("102.102.102.102",8888)));
HttpRequestBody
| API | 说明 |
| HttpRequestBody.form(Map<string,object> params, String encoding) |
使用表单提交的方式 |
| HttpRequestBody.json(String json, String encoding) |
使用JSON的方式,json是序列化后的结果 |
| HttpRequestBody.xml(String xml, String encoding) |
设置xml的方式,xml是序列化后的结果 |
| HttpRequestBody.custom(byte[] body, String contentType, String encoding) |
设置自定义的requestBody
|
组件的使用
| 方法 | 说明 | 示例 |
| setScheduler() |
设置Scheduler |
spipder.setScheduler(new FileCacheQueueScheduler("D:datawebmagic")) |
| setDownloader() |
设置Downloader |
spipder.setDownloader(new SeleniumDownloader())) |
| addPipeline() |
设置Pipeline,一个Spider可以有多个Pipeline |
spipder.addPipeline(new FilePipeline())
|
结果输出方式
| 类 | 说明 | 备注 |
| ConsolePipeline |
输出结果到控制台 |
抽取结果需要实现toString方法 |
| FilePipeline |
保存结果到文件 |
抽取结果需要实现toString方法 |
| JsonFilePipeline |
JSON格式保存结果到文件 |
|
| ConsolePageModelPipeline |
(注解模式)输出结果到控制台 |
|
| FilePageModelPipeline |
(注解模式)保存结果到文件 |
|
| JsonFilePageModelPipeline |
(注解模式)JSON格式保存结果到文件 |
想要持久化的字段需要有getter方法 |