WebMagic(一)-----初步使用

版权声明: https://blog.csdn.net/qq_39769369/article/details/82878034

参考文档:http://webmagic.io/docs/zh

1:创建maven项目(官方推荐使用maven)

我使用myeclipse创建后包结构如图所示:

也可以使用创建普通的Java项目导入相关jar包,如图所示

下载路径:https://download.csdn.net/download/qq_39769369/10692739

2、配置pom.xml ----引入webmagic-core、webmagic-extension

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.zhl.webmagic</groupId>
	<artifactId>java-webmagic</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>JavaWebMagic</name>
	<description>JavaWebMagic</description>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>us.codecraft</groupId>
			<artifactId>webmagic-core</artifactId>
			<version>0.5.3</version>
		</dependency>
		<dependency>
			<groupId>us.codecraft</groupId>
			<artifactId>webmagic-extension</artifactId>
			<version>0.5.3</version>
		</dependency>
		
	</dependencies>
</project>

3、使用官方例子

3.1写一个类实现PageProcessor接口的process()方法,在其中写逻辑代码

3.2在main函数中使用Spider作为入口

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class GithubRepoPageProcessor implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(100);

    @Override
    public void process(Page page) {
        page.addTargetRequests(page.getHtml().links().regex("(https://github\\.com/\\w+/\\w+)").all());
        page.putField("author", page.getUrl().regex("https://github\\.com/(\\w+)/.*").toString());
        page.putField("name", page.getHtml().xpath("//h1[@class='entry-title public']/strong/a/text()").toString());
        if (page.getResultItems().get("name")==null){
            //skip this page
            page.setSkip(true);
        }
        page.putField("readme", page.getHtml().xpath("//div[@id='readme']/tidyText()"));
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        
        Spider.create(new GithubRepoPageProcessor()).addUrl("https://github.com/code4craft").thread(5).run();
    }
}

3.3代码解析:

/*
	 * 为防止该页面请请求失败 
	 * setRetryTimes(3)设置该页面请求次数 
	 * setSleepTime(3000)设置请求次数间隔时间
	 */
	private Site site = Site.me().setRetryTimes(3).setSleepTime(100);
/*
		 * 抓取匹配符合该正则表达式的URL加入请求队列中
		 * .all()--返回的是个集合
		 */
		page.addTargetRequests(page.getHtml().links()
				.regex("(https://github\\.com/\\w+/\\w+)").all());
/**
		 * 抽取结果元素
		 */
		page.putField("author",
				page.getUrl().regex("https://github\\.com/(\\w+)/.*")
						.toString());
        /*
		 * Spider入口
		 * addUrl--添加爬取的地址
		 * thread(5).run()--5个线程去抓取
		 */
Spider.create(new GithubRepoPageProcessor())
				.addUrl("https://github.com/code4craft").thread(5).run();

3.4页面元素的抽取:

1、XPath

XPath本来是用于XML中获取元素的一种查询语言

2、CSS选择器

CSS选择器是与XPath类似的语言。如果大家做过前端开发,肯定知道$('h1.entry-title')这种写法的含义。客观的说,它比XPath写起来要简单一些,但是如果写复杂一点的抽取规则,就相对要麻烦一点。

3、正则表达式

正则表达式则是一种通用的文本抽取语言。

猜你喜欢

转载自blog.csdn.net/qq_39769369/article/details/82878034