webmagic 获取文本_通过Heritrix或者webmagic如何爬去指定url里的内容?

@wangxing.xu: 没听懂,爬虫技术通常是要深度爬取的,就是对二级链接继续爬取。只对url去解析,通常就是用jsoup,webmagic底层也是用jsoup去解析html的,不过是封装了深度爬取的步骤。

另外Document这个对象本身就是抽象的,他不仅仅可以容纳html,也能容纳json字符串等等。总之他能容纳url对应的响应内容。

如果你非要用爬虫技术,我给你贴一下webmagic的示例:

package com.scistor.datavision.operator.webcrawler.webmagic;

import us.codecraft.webmagic.Page;

import us.codecraft.webmagic.Site;

import us.codecraft.webmagic.Spider;

import us.codecraft.webmagic.pipeline.ConsolePipeline;

import us.codecraft.webmagic.processor.PageProcessor;

import us.codecraft.webmagic.selector.Html;

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://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()"));

Html html = page.getHtml();

// 部分三:从页面发现后续的url地址来抓取

page.addTargetRequests(page.getHtml().links()

.regex("(https://github\\.com/\\w+/\\w+)").all());

}

public Site getSite() {

return site;

}

public static void main(String[] args) {

Spider.create(new GithubRepoPageProcessor())

// 从"https://github.com/code4craft"开始抓

.addUrl("https://github.com/code4craft").addPipeline(new ConsolePipeline())

// 开启5个线程抓取

.thread(5)

// 启动爬虫

.run();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值