章节修改

main
sh00859 7 years ago
parent 90bccde83e
commit ab57a9ceb4

@ -0,0 +1,32 @@
package com.tamguo.model.vo;
import com.xuxueli.crawler.annotation.PageFieldSelect;
import com.xuxueli.crawler.annotation.PageSelect;
import java.util.List;
@PageSelect(cssQuery = ".out-chapter")
public class ChapterVo {
@PageFieldSelect(cssQuery = "h3")
private String name;
@PageFieldSelect(cssQuery = ".out-list li")
private List<String> sonChapters;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getSonChapters() {
return sonChapters;
}
public void setSonChapters(List<String> sonChapters) {
this.sonChapters = sonChapters;
}
}

@ -1,8 +1,8 @@
package com.tamguo.service.impl; package com.tamguo.service.impl;
import com.tamguo.dao.BookMapper; import com.tamguo.dao.ChapterMapper;
import com.tamguo.model.BookEntity; import com.tamguo.model.ChapterEntity;
import com.tamguo.model.vo.BookVo; import com.tamguo.model.vo.ChapterVo;
import com.tamguo.service.IBookService; import com.tamguo.service.IBookService;
import com.xuxueli.crawler.XxlCrawler; import com.xuxueli.crawler.XxlCrawler;
import com.xuxueli.crawler.parser.PageParser; import com.xuxueli.crawler.parser.PageParser;
@ -13,12 +13,15 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
import java.util.UUID;
@Service @Service
public class BookService implements IBookService { public class BookService implements IBookService {
@Autowired @Autowired
BookMapper bookMapper; ChapterMapper chapterMapper;
private Logger logger = LoggerFactory.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
@ -26,33 +29,44 @@ public class BookService implements IBookService {
@Override @Override
public void crawlerBook() { public void crawlerBook() {
XxlCrawler crawler = new XxlCrawler.Builder() XxlCrawler crawler = new XxlCrawler.Builder()
.setUrls("http://www.ruiwen.com/jiaocai/") .setUrls("https://tiku.baidu.com/tikupc/chapterlist/1bfd700abb68a98271fefa04-27-jiaocai-11")
.setAllowSpread(false) .setAllowSpread(false)
.setFailRetryCount(5) .setFailRetryCount(5)
.setThreadCount(20) .setThreadCount(20)
.setPageParser(new PageParser<BookVo>() { .setPageParser(new PageParser<ChapterVo>() {
@Override @Override
public void parse(Document html, Element pageVoElement, BookVo bookVo) { public void parse(Document html, Element pageVoElement, ChapterVo chapterVo) {
// 解析封装 PageVo 对象 // 解析封装 PageVo 对象
String pageUrl = html.baseUri(); String parentName = chapterVo.getName();
if (pageUrl.equals("http://www.ruiwen.com/jiaocai/")) { ChapterEntity chapterEntity = new ChapterEntity();
logger.info("开始解析书本信息:{}", pageUrl); String uid = UUID.randomUUID().toString().replace("-", "");
String name = bookVo.getName(); chapterEntity.setUid(uid);
String url = bookVo.getBookUrl(); chapterEntity.setName(parentName);
chapterEntity.setCourseId("0");
BookEntity bookEntity = new BookEntity(); chapterEntity.setCourseId("0");
bookEntity.setName(name); chapterEntity.setParentId("-1");
bookEntity.setReserveField1(url); chapterEntity.setQuestionNum(0);
bookEntity.setQuestionNum(0); chapterEntity.setPointNum(0);
bookEntity.setPointNum(0); chapterMapper.insert(chapterEntity);
bookMapper.insert(bookEntity);
}
List<String> sonChapters = chapterVo.getSonChapters();
sonChapters.forEach(s -> {
ChapterEntity sonChapterEntity = new ChapterEntity();
sonChapterEntity.setName(s);
sonChapterEntity.setCourseId("0");
sonChapterEntity.setCourseId("0");
sonChapterEntity.setParentId(uid);
sonChapterEntity.setQuestionNum(0);
sonChapterEntity.setPointNum(0);
chapterMapper.insert(sonChapterEntity);
});
} }
// }
}).build(); }).build();
// runData = crawler.getRunData();
// 获取科目 // 获取科目
crawler.start(true); crawler.start(true);
} }

Loading…
Cancel
Save