jsoup解析html/根据关键词拿到论坛帖子信息
2013-04-19 16:58
267 查看
以论坛《宽带山》为例,需要根据给定的关键词,取得关于该关键词的所有帖子,包括人气数,发帖主题,回复数,发表人,发表时间,帖子链接,帖子详细文本内容等。
详细代码如下:
Java代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class KeyWordsSearchUtil {
/**
* 根据关键词查询论坛所需信息map
* @param KeyWord 传入关键词
* @return
*/
public static List<Map<String, Object>> findByKeyWord(String KeyWord) {
List<Map<String, Object>>postsList=new ArrayList<Map<String,Object>>();
Map<String, Object>postsOneMap=null;
try {
Document doc = Jsoup.connect("http://club.pchome.net/forum_1_15____md__1_"+java.net.URLEncoder.encode(KeyWord,"utf-8")+".html")
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(10000)
.ignoreHttpErrors(true)
.post();
Elements postsLs=doc.select("li.i2").not(".h-bg");
if (postsLs!=null&&postsLs.size()>0) {
for (Element childPost : postsLs) {
postsOneMap=new HashMap<String, Object>();
postsOneMap.put("postsPopularity", childPost.select("li > span.n2").first().text());
postsOneMap.put("postsTitle", childPost.select("span.n3 > a").attr("title"));
postsOneMap.put("postsFloor", childPost.select("span.n4").first().text());
postsOneMap.put("postsCname", childPost.select("a.bind_hover_card").first().text());
postsOneMap.put("postsCtime", childPost.select("li > span.n6").first().text());
postsOneMap.put("postsUrl", "http://club.pchome.net"+childPost.select("span.n3 a").attr("href"));
postsOneMap.put("postsContents", getContentsByUrl("http://club.pchome.net"+childPost.select("span.n3 a").attr("href")));
postsList.add(postsOneMap);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return postsList;
}
/**
* 根据帖子的url获取帖子的文本内容
* @param url 帖子的路径
* @return
*/
public static String getContentsByUrl(String url) {
String contents="11";
try {
Document doc = Jsoup.connect(url)
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(10000)
.ignoreHttpErrors(true)
.post();
if(doc.select("div.mc").first()!=null){
Element contentsEle=doc.select("div.mc div").first();
contents=contentsEle.select("div").first().text();
if (contents.contains("[向左转] [向右转] [原图]")) {
contents=contents.replace("[向左转] [向右转] [原图]", "");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return contents;
}
public static void main(String[] args) throws Exception {
List<Map<String, Object>>postsList=KeyWordsSearchUtil.findByKeyWord("电影");
System.out.println("http://club.pchome.net/forum_1_15____md__1_"+java.net.URLEncoder.encode("电影","utf-8")+".html");
System.out.println(postsList.size()+"/////");
for (int i = 0; i < postsList.size(); i++) {
for(Map.Entry<String, Object>entry:postsList.get(i).entrySet()){
System.out.println("key="+entry.getKey()+"| value="+entry.getValue());
}
System.out.println("-----------------");
}
//http://club.pchome.net/thread_1_15_7519679.html
// String str=getContentsByUrl("http://club.pchome.net/thread_1_15_7519679.html");
// System.out.println(str);
}
}
以上代码能成功抓取宽带山论坛中,关键词为:电影 的相关帖子列表,main方法中已有测试,网络畅通下可测试通过。但上面代码仅为完成功能,性能较差,项目中需重写或优化
详细代码如下:
Java代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class KeyWordsSearchUtil {
/**
* 根据关键词查询论坛所需信息map
* @param KeyWord 传入关键词
* @return
*/
public static List<Map<String, Object>> findByKeyWord(String KeyWord) {
List<Map<String, Object>>postsList=new ArrayList<Map<String,Object>>();
Map<String, Object>postsOneMap=null;
try {
Document doc = Jsoup.connect("http://club.pchome.net/forum_1_15____md__1_"+java.net.URLEncoder.encode(KeyWord,"utf-8")+".html")
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(10000)
.ignoreHttpErrors(true)
.post();
Elements postsLs=doc.select("li.i2").not(".h-bg");
if (postsLs!=null&&postsLs.size()>0) {
for (Element childPost : postsLs) {
postsOneMap=new HashMap<String, Object>();
postsOneMap.put("postsPopularity", childPost.select("li > span.n2").first().text());
postsOneMap.put("postsTitle", childPost.select("span.n3 > a").attr("title"));
postsOneMap.put("postsFloor", childPost.select("span.n4").first().text());
postsOneMap.put("postsCname", childPost.select("a.bind_hover_card").first().text());
postsOneMap.put("postsCtime", childPost.select("li > span.n6").first().text());
postsOneMap.put("postsUrl", "http://club.pchome.net"+childPost.select("span.n3 a").attr("href"));
postsOneMap.put("postsContents", getContentsByUrl("http://club.pchome.net"+childPost.select("span.n3 a").attr("href")));
postsList.add(postsOneMap);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return postsList;
}
/**
* 根据帖子的url获取帖子的文本内容
* @param url 帖子的路径
* @return
*/
public static String getContentsByUrl(String url) {
String contents="11";
try {
Document doc = Jsoup.connect(url)
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(10000)
.ignoreHttpErrors(true)
.post();
if(doc.select("div.mc").first()!=null){
Element contentsEle=doc.select("div.mc div").first();
contents=contentsEle.select("div").first().text();
if (contents.contains("[向左转] [向右转] [原图]")) {
contents=contents.replace("[向左转] [向右转] [原图]", "");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return contents;
}
public static void main(String[] args) throws Exception {
List<Map<String, Object>>postsList=KeyWordsSearchUtil.findByKeyWord("电影");
System.out.println("http://club.pchome.net/forum_1_15____md__1_"+java.net.URLEncoder.encode("电影","utf-8")+".html");
System.out.println(postsList.size()+"/////");
for (int i = 0; i < postsList.size(); i++) {
for(Map.Entry<String, Object>entry:postsList.get(i).entrySet()){
System.out.println("key="+entry.getKey()+"| value="+entry.getValue());
}
System.out.println("-----------------");
}
//http://club.pchome.net/thread_1_15_7519679.html
// String str=getContentsByUrl("http://club.pchome.net/thread_1_15_7519679.html");
// System.out.println(str);
}
}
以上代码能成功抓取宽带山论坛中,关键词为:电影 的相关帖子列表,main方法中已有测试,网络畅通下可测试通过。但上面代码仅为完成功能,性能较差,项目中需重写或优化
相关文章推荐
- Jsoup解析html实现招聘信息查询功能
- 菜鸟练习C#htmlparser----C#正则加htmlDOM进行网页解析腾讯新闻帖子列表相关信息提取
- jsoup 解析HTML信息
- jsoup 解析HTML信息
- [Android实例] 利用Jsoup解析Html实现的Android招聘信息大全
- jsoup 解析HTML信息
- 菜鸟练习C#htmlparser----C#正则加htmlDOM进行网页解析腾讯新闻帖子列表相关信息提取
- 使用jsoup解析html的table中的文本信息
- 根据关键词查询论坛所需信息
- jsoup解析html实现招聘信息查询
- 使用jsoup解析html的table中的文本信息实例
- HttpClient+Jsoup模拟登陆,解析HTML,信息筛选(广工图书馆)
- 使用 jsoup 对 HTML 文档进行解析和操作
- 使用Jsoup解析html网页
- Android用jsoup解析html
- 路透社新闻 --- jsoup解析html
- Jsoup解析HTML时特殊标签的解析
- JSoup解析html常见问题
- 使用 jsoup 对 HTML 文档进行解析和操作
- Android利用Jsoup解析html 开发网站客户端小记。