1.WebMagic 官方文檔地址 http://webmagic.io/docs/zh/
引入WebMagic的jar 這裏採用pom形式
<!-- 使用webmagic所用的jar -->
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-core</artifactId>
<version>0.7.3</version>
</dependency>
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-extension</artifactId>
<version>0.7.3</version>
</dependency>
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-extension</artifactId>
<version>0.7.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
2.開始第一個爬蟲,我這是用了官方文檔的事例,由於github我這好像訪問不了,就給了一下訪問地址 就是我的博客地址
代碼如下 ,部分註釋自帶
package com.test;
import java.util.List;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
public class GithubRepoPageProcessor implements PageProcessor {
// 部分一:抓取網站的相關配置,包括編碼、抓取間隔、重試次數等
private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);
// process是定製爬蟲邏輯的核心接口,在這裏編寫抽取邏輯
public void process(Page page) {
// 部分二:定義如何抽取頁面信息,並保存下來
page.putField("author", page.getUrl().regex("https://blog.csdn\\.net/(\\w+)/.*").toString());
page.putField("name", page.getHtml().xpath("//div[@class='push-event']/strong/a/text()").toString());
System.out.println();
List<String> contextList=page.getHtml().xpath("h4[@class='text-truncate']/a/text()").all();
for(String str:contextList){
System.out.println(str);
}
//System.out.println( page.getHtml().regex("h4[@class='text-truncate']/a/text()").all().toString());
if (page.getResultItems().get("name") == null) {
//skip this pagetimeline-on date-point
page.setSkip(true);
}
page.putField("readme", page.getHtml().xpath("//div[@id='readme']/tidyText()"));
// 部分三:從頁面發現後續的url地址來抓取
//page.addTargetRequests(page.getHtml().links().regex("(https://blog.csdn\\.net/[\\w\\-]+/[\\w\\-]+)").all());
}
public Site getSite() {
return site;
}
public static void main(String[] args) {
Spider.create(new GithubRepoPageProcessor())
//從"https://github.com/code4craft"開始抓
.addUrl("https://blog.csdn.net/hanzl1")
.addPipeline(new JsonFilePipeline("E:\\webmagic\\"))
//開啓5個線程抓取
.thread(5)
//啓動爬蟲
.run();
}
}
3. 設置起始的url https://blog.csdn.net/hanzl1
.addUrl("https://blog.csdn.net/hanzl1")
分析了一下要抓的頁面的信息 就是想抓一下頁面的標題
使用代碼
運行結果對比 :
4.使用了這個框架後其實最主要的難點是分析內容 還有好多東西要
4.1.2 頁面元素的抽取
第二部分是爬蟲的核心部分:對於下載到的Html頁面,你如何從中抽取到你想要的信息?WebMagic裏主要使用了三種抽取技術:XPath、正則表達式和CSS選擇器。另外,對於JSON格式的內容,可使用JsonPath進行解析。
-
XPath
XPath本來是用於XML中獲取元素的一種查詢語言,但是用於Html也是比較方便的。例如:
page.getHtml().xpath("//h1[@class='entry-title public']/strong/a/text()")
這段代碼使用了XPath,它的意思是“查找所有class屬性爲'entry-title public'的h1元素,並找到他的strong子節點的a子節點,並提取a節點的文本信息”。 對應的Html是這樣子的:
-
CSS選擇器
CSS選擇器是與XPath類似的語言。如果大家做過前端開發,肯定知道$('h1.entry-title')這種寫法的含義。客觀的說,它比XPath寫起來要簡單一些,但是如果寫複雜一點的抽取規則,就相對要麻煩一點。 運行效果是一樣的 如: List<String> contextList2=page.getHtml().css("h4[class='text-truncate'] a","text").all();
-
正則表達式
正則表達式則是一種通用的文本抽取語言。
page.addTargetRequests(page.getHtml().links().regex("(https://github\\.com/\\w+/\\w+)").all());
這段代碼就用到了正則表達式,它表示匹配所有"https://github.com/code4craft/webmagic"這樣的鏈接。我覺得這塊挺難得,正則不太熟。
-
JsonPath