2019独角兽企业重金招聘Python工程师标准>>>
前言
- 上次写到niconico爬虫,今天上一个bilibili爬虫,使用的依然是webmagic.
webmagic简介地址(请点击)
注意
-
常规网页展示部分一般分为列表页和详情页,bilibili站是请求后台api返回json,然后使用js生成网页,所有主要设计解析方法为,json解析处理。
-
b站后端json返回格式相对n站更具结构化,清晰,但是要注意能用字符类型尽量用字符串类型,兼容性更好。
-
本人的springboot 是采用多线程定时器,分别定时跑爬虫数据和下载爬虫资源,以后会在这里慢慢列出解决方法。
-
b站评论和列表可以用api请求得出,但是详情真实地址需不能直获取都加密,可以借助地三处理获取真实数据http://flvurl.cn/ ,另外B站图片和视频均匀简单防倒链,图片,需要。图片需要或删除Referer,这里还有H5的小技巧,以后慢慢说。视频需要 Referer:http://www.bilibili.com/
-
SpiderNicoService为一些数据的入库处理,SslDownloader为webmagic获取https,请求处理。
pom.xml配置
us.codecraft
webmagic-core
0.5.2
org.slf4j
slf4j-log4j12
process
package win.raychow.modules.spider.base.processor;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import win.raychow.demo.spider.tool.SslDownloader;
import win.raychow.modules.spider.base.dao.SpiderBili;
import win.raychow.modules.spider.base.domain.BilibiliReplyModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by ray on 2017/7/2.
* 爬虫进程
*/
@Service
public class BilibiliProcessor implements PageProcessor {
//https://api.bilibili.com/archive_rank/getarchiverankbypartion?type=jsonp&tid=20&pn=1
private String bashUrl = "https://api.bilibili.com/archive_rank/getarchiverankbypartion?type=jsonp&tid=20&pn=" ;// + i
//https://www.bilibili.com/video/av11778873/
private String detailUrl = "https://www.bilibili.com/video/av";
//https://api.bilibili.com/x/v2/reply?jsonp=jsonp&pn=1&type=1&oid=11253064
private String replyUrl = "https://api.bilibili.com/x/v2/reply?jsonp=jsonp&pn=1&type=1&oid=";
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${spider.bilibili.maxSize}")
int maxSize;
@Autowired
BilibiliPipeLine pipeLine;
private Site site = Site.me()
.setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
.setSleepTime(10 * 1000)
.setTimeOut(20 * 1000)
.setRetryTimes(3)
.setCycleRetryTimes(3);
@Override
public Site getSite() {
return site;
}
@Override
public void process(Page page){
String pageUrl = page.getUrl().toString();
logger.info(pageUrl);
try {
if (pageUrl.contains(bashUrl)) {
//列表页
String text = page.getJson().toString();
JSONObject jsonObject = JSONObject.parseObject(text);
JSONObject archives = (JSONObject) ((JSONObject) jsonObject.get("data")).get("archives");
List list = new ArrayList<>();
List reqList = new ArrayList<>();
for (Map.Entry entry : archives.entrySet()) {
SpiderBili tmp = JSON.parseObject(entry.getValue().toString(),SpiderBili.class);
list.add(tmp);
String reqTmp = replyUrl + tmp.getAid();
reqList.add(reqTmp);
}
if (list.size() > 0){
page.putField("type", 0);
page.putField("data", list);
page.addTargetRequests(reqList);
}
}
else if (pageUrl.contains(replyUrl)){
String text = page.getJson().toString();
if (text.length() > 10){
BilibiliReplyModel replyModel = JSON.parseObject(text,BilibiliReplyModel.class);
String aid = pageUrl.replace(replyUrl,"");
page.putField("type", 1);
page.putField("data", text);
page.putField("aid", aid);
}
}
else if (pageUrl.contains(detailUrl)) {
//详情页
}
}
catch (Exception e){
logger.error("url:" + pageUrl );
}
}
public void run(){
Spider spider = Spider.create(new BilibiliProcessor())
.setDownloader(new SslDownloader())
.addPipeline(pipeLine);
//.addPipeline(new ConsolePipeline());//打印到控制台
spider.addUrl("https://api.bilibili.com/archive_rank/getarchiverankbypartion?type=jsonp&tid=20&pn=32");
for (int i = 1; i < maxSize; i++) { //4000
String tmp = bashUrl + i;
spider.addUrl(tmp);
}
try {
spider.run();
}
catch (Exception e){
}
}
}
pipeline
package win.raychow.modules.spider.base.processor;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import win.raychow.modules.spider.base.dao.SpiderBili;
import win.raychow.modules.spider.base.dao.SpiderBiliService;
import java.util.List;
/**
* Created by ray on 2017/6/18.
* 爬虫管道
*/
@Service
public class BilibiliPipeLine implements Pipeline {
@Autowired
SpiderBiliService service;
@Override
public void process(ResultItems resultItems, Task task){
if (resultItems.getAll().isEmpty() == false) {
int type = resultItems.get("type");
if (type == 0){
//列表内容
List list = resultItems.get("data");
for (SpiderBili obj: list) {
service.updateBySpider(obj);
}
}
else if(type == 1){
//评论内容
int aid = Integer.parseInt(resultItems.get("aid"));
String string = resultItems.get("data");
JSONObject jsonObject = JSON.parseObject(string);
String json = JSON.toJSONString(jsonObject);
List list = service.findByAid(aid);
//之前有回复主题
if (list.size() > 0){
SpiderBili bili = list.get(0);
bili.setReplyJson(json);
service.update(bili);
}
}
}
}
}
原文:http://raychow.linkfun.top/2017/12/08/archives/9_javaSpring/spriderBiliBili/index/