newselectors
parent
b1cba78bd6
commit
55d4a76ab7
@ -0,0 +1,23 @@
|
||||
package us.codecraft.webmagic.selector;
|
||||
|
||||
import org.jsoup.Jsoup;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author code4crafter@gmail.com
|
||||
* @since 0.2.2
|
||||
*/
|
||||
public abstract class BaseElementSelector implements Selector,ElementSelector {
|
||||
|
||||
@Override
|
||||
public String select(String text) {
|
||||
return select(Jsoup.parse(text));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> selectList(String text) {
|
||||
return selectList(Jsoup.parse(text));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package us.codecraft.webmagic.selector;
|
||||
|
||||
import org.jsoup.nodes.Element;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Selector(extractor) for html elements.<br>
|
||||
*
|
||||
* @author code4crafter@gmail.com <br>
|
||||
* @since 0.2.2
|
||||
*/
|
||||
public interface ElementSelector {
|
||||
|
||||
/**
|
||||
* Extract single result in text.<br>
|
||||
* If there are more than one result, only the first will be chosen.
|
||||
*
|
||||
* @param element
|
||||
* @return result
|
||||
*/
|
||||
public String select(Element element);
|
||||
|
||||
/**
|
||||
* Extract all results in text.<br>
|
||||
*
|
||||
* @param element
|
||||
* @return results
|
||||
*/
|
||||
public List<String> selectList(Element element);
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package us.codecraft.webmagic.selector;
|
||||
|
||||
import org.jsoup.nodes.Element;
|
||||
import us.codecraft.xsoup.XPathEvaluator;
|
||||
import us.codecraft.xsoup.Xsoup;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* XPath selector based on Xsoup.<br>
|
||||
*
|
||||
* @author code4crafter@gmail.com <br>
|
||||
* @since 0.2.2
|
||||
*/
|
||||
public class XsoupSelector extends BaseElementSelector {
|
||||
|
||||
private XPathEvaluator xPathEvaluator;
|
||||
|
||||
public XsoupSelector(String xpathStr) {
|
||||
this.xPathEvaluator = Xsoup.compile(xpathStr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String select(Element element) {
|
||||
return xPathEvaluator.evaluate(element).get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> selectList(Element element) {
|
||||
return xPathEvaluator.evaluate(element).list();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue