jvppeteer
jvppeteer copied to clipboard
[Feature] 支持xpath/selector提取元素值
引申说明
Page
$x,$,$$等api, 只能提取到相关的元素做type/click/etc操作, 而不能提取到元素的属性或者值(可能是我没查阅到相关api).
CrawlerTaobaoItem
示例中提取商品信息等操作都是通过正则匹配的方式.
是否需要引入Jsoup, Xsoup依赖, 对page#content做解析提取操作.
final String extractValue = Xsoup.compile("").evaluate(Jsoup.parse(pages.content())).get();
public List<ElementHandle> $x(String expression) {
return this.mainFrame().$x(expression);
}
========================================================================
public Pair<ElementHandle, String> $x(String expression) {
return ImmutablePair.of(this.mainFrame().$x(expression).get(0),
Xsoup.compile(expression).evaluate(Jsoup.parse(pages.content())).get()
);
}
不需要呀,api提供了获取元素具体属性的,通用的方式如下:
public static String getElementAttr(ElementHandle element, String param) { try { JSHandle jsHandle = element.getProperty(param); if (null == jsHandle) return ""; return jsHandle.jsonValue().toString(); } catch (Exception e) { return ""; } }
感谢回复, 不过xpath涵盖了大量的函数, 常用的text/position/last/count, 需要获取的是函数返回值并非元素具体属性. 可能我对该依赖库的Api不太了解, 如果有更好的做法, 不吝赐教.