我正试图玩一个自动化的在线游戏,但我被卡住了。
我想做的事情:我正在努力分析我的报告。第一步是获取父行。第二步是根据报表是否已读取以及报表是否为今天的日期来筛选父行。
this.driver.findElements(this.reportRow).stream()
.filter(reportElement -> reportElement.findElement(this.unreadReports).isDisplayed()
&& reportElement.findElement(this.todaysReport).getText().contains(this.bountyDate))
.map(reportRow -> {
String villageName = reportRow.findElement(this.unreadReports).getText().replace("mwldjt raids ", "");
String bounty = reportRow.findElement(this.bountyElement).getText();
System.out.println("Village: " + villageName + " Bounty: " + bounty);
return new String[]{bounty, villageName};
})
.forEach(entry -> System.out.println("Entry:" + entry[0] + ", " + entry[1])
这是我试图解析的HTML。
<tr>
<td class="sel">
<input class="check" name="n1" type="checkbox" value="14180678">
</td>
<td class="sub newMessage">
<a href="berichte.php?id=14180678&t=1&s=0&page=1&toggleState=0">
<img alt="unread" class="messageStatus messageStatusUnread" src="img/x.gif">
</a>
<img alt="Won as attacker with losses." class="iReport iReport2 " src="img/x.gif">
<a class="reportInfoIcon" href="build.php?id=39&tt=2&bid=14180678"><img alt="16/100" class="reportInfo carry half"
src="img/x.gif"></a>
<div class="">
<a href="berichte.php?id=14180678%7C5fa6b3d7&t=1&s=1">mwldjt raids Natars 36|64</a>
</div>
<div class="clear"></div>
</td>
<td class="dat">
24/04/20, 04:08 am
</td>
</tr>
我正试图在LinkedHashmap中找到赏金和村庄名称。我现在已经用打印方法替换了foreach块中的"put"。
问题:
我需要执行两个操作——从父行中的两个元素获取数据。我一直在努力了解如何做到这一点,但现在我想我需要帮助。谢谢你抽出时间。
在LinkedHashMap
上进行put
操作的forEach
可以转换为使用
.collect(Collectors.toMap(<key-fn>, <value-fn>, <merge-fn>, LinkedHashMap::new)
所以你已经分片的代码可以重构到中
LinkedHashMap<String, String> output = this.driver.findElements(this.reportRow).stream()
.filter(reportElement -> reportElement.findElement(this.unreadReports).isDisplayed()
&& reportElement.findElement(this.todaysReport).getText().contains(this.bountyDate))
.map(reportRow -> {
String villageName = reportRow.findElement(this.unreadReports).getText().replace("mwldjt raids ", "");
String bounty = reportRow.findElement(this.bountyElement).getText();
System.out.println("Village: " + villageName + " Bounty: " + bounty);
return new String[]{bounty, villageName};
})
.collect(Collectors.toMap(e -> e[0], e -> e[1], (a,b) -> b, LinkedHashMap::new);
当然,获取村庄名称和赏金的操作可以提取出来,并以以下形式直接放置在toMap
收集器中:
LinkedHashMap<String, String> output = this.driver.findElements(this.reportRow).stream()
.filter(reportElement -> isValidReportElement(reportElement))
.collect(Collectors.toMap(reportElement -> getVillageName(reportElement),
reportElement -> getBounty(reportElement), (a,b) -> b, LinkedHashMap::new);