forked from 99246255/SpringBoot-Solr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoApplicationTests.java
More file actions
38 lines (33 loc) · 1.18 KB
/
DemoApplicationTests.java
File metadata and controls
38 lines (33 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.solr;
import com.solr.webmagic.JDPipeline;
import com.solr.webmagic.JDProductProcessor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.monitor.SpiderMonitor;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
JDPipeline jdPipeline;
/**
* 京东爬虫
* @throws Exception
*/
@Test
public void testJDProductProcessor() throws Exception{
//chromedriver.exe需与浏览器版本对应
String chromeDriverPath = JDProductProcessor.class.getClassLoader().getResource("chromedriver.exe").getFile();
Spider jdSpider = Spider.create(new JDProductProcessor())
.addUrl("http://www.jd.com/allSort.aspx")// JD全部分类
.addPipeline(jdPipeline)
// .setDownloader(new SeleniumDownloader(chromeDriverPath))// 可用谷歌浏览器,默认Downloader基于HttpClient
.thread(5);
// 注册爬虫监控
SpiderMonitor.instance().register(jdSpider);
jdSpider.run();
}
}