링크 공유 자바 구현 (제목 캡 처 및 설명 정보)
1749 단어 자바 구현
package com.jyeba.core.html;
public class HtmlInfo {
private String title;
private String desc;
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getDesc() {
return desc;
}
}
package com.jyeba.core.html;
public class HtmlTools {
public static HtmlInfo getHtmlInfo(String url) throws IOException {
HtmlInfo html = new HtmlInfo();
Document doc = Jsoup.connect(url)
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(6000)
.get();
Elements e = doc.select("title");
if (e.size() > 0) {
System.out.println(e.text());
html.setTitle(e.text());
}
e = doc.select("meta[name=Description]");
if (e.size() > 0) {
System.out.println(e.get(0).attr("content"));
html.setDesc(e.get(0).attr("content"));
}
return html;
}
public static void main(String[] args) throws IOException{
HtmlInfo info=HtmlTools.getHtmlInfo("http://news.qq.com/a/20111017/000091.htm");
}
}