139 lines
3.1 KiB
Java
139 lines
3.1 KiB
Java
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.net.URL;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
import org.jsoup.Jsoup;
|
|
import org.jsoup.nodes.Document;
|
|
import org.jsoup.nodes.Element;
|
|
import org.jsoup.select.Elements;
|
|
|
|
|
|
public class JQueryUtils {
|
|
public static Elements select(String selector, String content) {
|
|
content = content.replace('\0', ' ');
|
|
Document doc = Jsoup.parse(content);
|
|
Elements elements = doc.body().select(selector);
|
|
return elements;
|
|
}
|
|
|
|
public static Elements select(String selector, Document doc) {
|
|
return select(Arrays.asList(selector), doc);
|
|
}
|
|
|
|
public static Elements select(List<String> selectors, Document doc) {
|
|
for(String selector : selectors) {
|
|
Elements elements = doc.body().select(selector);
|
|
if(elements != null && elements.size() > 0) {
|
|
return elements;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static Elements select(String selector, String content, String url) {
|
|
content = content.replace('\0', ' ');
|
|
Document doc = Jsoup.parse(content, url);
|
|
Elements elements = doc.body().select(selector);
|
|
return elements;
|
|
}
|
|
|
|
private static Document getDocFromUrl(String url){
|
|
Document doc = null;
|
|
for(int repeatTimes =0;repeatTimes<3&&doc==null;repeatTimes++){
|
|
try{
|
|
if(repeatTimes>0){
|
|
Thread.sleep(1000);
|
|
System.out.println( "准备尝试第"+(repeatTimes+1)+"次请求:$url");
|
|
}
|
|
doc = Jsoup.parse(new URL(url), 5*1000);
|
|
if(doc != null){
|
|
break;
|
|
}
|
|
}catch(Exception e){
|
|
|
|
}
|
|
|
|
}
|
|
if(doc==null){
|
|
System.out.println("检测到不可访问链接:$url");
|
|
}
|
|
return doc;
|
|
}
|
|
private static Document getDocFromUrl(String url,String charset){
|
|
Document doc = null;
|
|
for(int repeatTimes =0;repeatTimes<3&&doc==null;repeatTimes++){
|
|
InputStream input = null;
|
|
try{
|
|
if(repeatTimes>0){
|
|
Thread.sleep(1000);
|
|
System.out.println("准备尝试第"+(repeatTimes+1)+"次请求:$url");
|
|
}
|
|
URL _url = new URL(url);
|
|
input = _url.openStream();
|
|
doc = Jsoup.parse(input, charset, url);
|
|
if(doc != null){
|
|
break;
|
|
}
|
|
}catch(Exception e){
|
|
|
|
}finally{
|
|
if(input!=null){
|
|
try {
|
|
input.close();
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
if(doc==null){
|
|
System.out.println("检测到不可访问链接:$url");
|
|
}
|
|
return doc;
|
|
}
|
|
|
|
public static String selectText(String selector, String content) {
|
|
Elements elements = JQueryUtils.select(selector, content);
|
|
|
|
String ret= "";
|
|
if (elements != null && elements.size() > 0) {
|
|
for (Element element : elements) {
|
|
ret = element.text();
|
|
break;
|
|
}
|
|
}
|
|
|
|
return ret.trim();
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String selectText(List<String> selectors, Document doc) {
|
|
if(selectors == null || selectors.size() == 0) {
|
|
return null;
|
|
}
|
|
|
|
String ret = null;
|
|
for(String selector : selectors) {
|
|
Elements elements = doc.body().select(selector);
|
|
if(elements == null || elements.size() == 0) {
|
|
continue;
|
|
}
|
|
|
|
for (Element element : elements) {
|
|
ret = element.text();
|
|
if(ret != null) {
|
|
return ret;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|