Skip to content

Commit 08f4a40

Browse files
committed
Update: 提供测试用例。
1 parent 12ce864 commit 08f4a40

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

webmagic-saxon/src/test/java/us/codecraft/webmagic/selector/XpathSelectorTest.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,12 +1393,13 @@ public void testXpath2Selector() {
13931393
public void htmlCleanerParseTest() {
13941394
Spider.create(new RuoxiaPageProcessor()).addUrl("http://www.ruoxia.com/top/dianji/month").thread(1).run();
13951395
}
1396+
13961397
class RuoxiaPageProcessor implements PageProcessor {
13971398
@Override
13981399
public void process(Page page) {
1399-
List<Selectable> nodes = page.getHtml().xpath("//div[@class=\"bd\"]//tbody/tr").nodes();
1400-
for (Selectable node:nodes) {
1401-
String name = node.xpath("//td[3]/div/a[1]/text()").get();
1400+
List<String> items = new Xpath2Selector("//div[@class=\"bd\"]//tbody/tr").selectList(page.getRawText());
1401+
for (String item : items) {
1402+
String name = new Xpath2Selector("//td[3]/div/a[1]/text()").select(item);
14021403
System.out.println(name);
14031404
}
14041405
}
@@ -1408,31 +1409,31 @@ public void process(Page page) {
14081409
@Test
14091410
public void performanceTest() {
14101411
Xpath2Selector xpath2Selector = new Xpath2Selector("//a");
1411-
long time =System.currentTimeMillis();
1412+
long time = System.currentTimeMillis();
14121413
for (int i = 0; i < 1000; i++) {
14131414
xpath2Selector.selectList(html);
14141415
}
1415-
System.out.println(System.currentTimeMillis()-time);
1416+
System.out.println(System.currentTimeMillis() - time);
14161417

14171418
XpathSelector xpathSelector = new XpathSelector("//a");
1418-
time =System.currentTimeMillis();
1419+
time = System.currentTimeMillis();
14191420
for (int i = 0; i < 1000; i++) {
14201421
xpathSelector.selectList(html);
14211422
}
1422-
System.out.println(System.currentTimeMillis()-time);
1423+
System.out.println(System.currentTimeMillis() - time);
14231424

1424-
time =System.currentTimeMillis();
1425+
time = System.currentTimeMillis();
14251426
for (int i = 0; i < 1000; i++) {
14261427
xpath2Selector.selectList(html);
14271428
}
14281429
System.out.println(System.currentTimeMillis() - time);
14291430

14301431
CssSelector cssSelector = new CssSelector("a");
1431-
time =System.currentTimeMillis();
1432+
time = System.currentTimeMillis();
14321433
for (int i = 0; i < 1000; i++) {
14331434
cssSelector.selectList(html);
14341435
}
1435-
System.out.println("css "+(System.currentTimeMillis()-time));
1436+
System.out.println("css " + (System.currentTimeMillis() - time));
14361437
}
14371438

14381439
@Ignore("take long time")
@@ -1444,54 +1445,54 @@ public void parserPerformanceTest() throws XPatherException {
14441445
TagNode tagNode = htmlCleaner.clean(html);
14451446
Document document = Jsoup.parse(html);
14461447

1447-
long time =System.currentTimeMillis();
1448+
long time = System.currentTimeMillis();
14481449
for (int i = 0; i < 2000; i++) {
14491450
htmlCleaner.clean(html);
14501451
}
1451-
System.out.println(System.currentTimeMillis()-time);
1452+
System.out.println(System.currentTimeMillis() - time);
14521453

1453-
time =System.currentTimeMillis();
1454+
time = System.currentTimeMillis();
14541455
for (int i = 0; i < 2000; i++) {
14551456
tagNode.evaluateXPath("//a");
14561457
}
1457-
System.out.println(System.currentTimeMillis()-time);
1458+
System.out.println(System.currentTimeMillis() - time);
14581459

14591460
System.out.println("=============");
14601461

1461-
time =System.currentTimeMillis();
1462+
time = System.currentTimeMillis();
14621463
for (int i = 0; i < 2000; i++) {
14631464
Jsoup.parse(html);
14641465
}
1465-
System.out.println(System.currentTimeMillis()-time);
1466+
System.out.println(System.currentTimeMillis() - time);
14661467

1467-
time =System.currentTimeMillis();
1468+
time = System.currentTimeMillis();
14681469
for (int i = 0; i < 2000; i++) {
14691470
document.select("a");
14701471
}
1471-
System.out.println(System.currentTimeMillis()-time);
1472+
System.out.println(System.currentTimeMillis() - time);
14721473

14731474
System.out.println("=============");
14741475

1475-
time =System.currentTimeMillis();
1476+
time = System.currentTimeMillis();
14761477
for (int i = 0; i < 2000; i++) {
14771478
htmlCleaner.clean(html);
14781479
}
1479-
System.out.println(System.currentTimeMillis()-time);
1480+
System.out.println(System.currentTimeMillis() - time);
14801481

1481-
time =System.currentTimeMillis();
1482+
time = System.currentTimeMillis();
14821483
for (int i = 0; i < 2000; i++) {
14831484
tagNode.evaluateXPath("//a");
14841485
}
1485-
System.out.println(System.currentTimeMillis()-time);
1486+
System.out.println(System.currentTimeMillis() - time);
14861487

14871488
System.out.println("=============");
14881489

14891490
XPathEvaluator compile = Xsoup.compile("//a");
1490-
time =System.currentTimeMillis();
1491+
time = System.currentTimeMillis();
14911492
for (int i = 0; i < 2000; i++) {
14921493
compile.evaluate(document);
14931494
}
1494-
System.out.println(System.currentTimeMillis()-time);
1495+
System.out.println(System.currentTimeMillis() - time);
14951496

14961497
}
14971498

0 commit comments

Comments
 (0)