javascript代码实例教程-jsoup解析html/根据关键词拿到论坛帖子信息

发布时间:2019-05-10 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-jsoup解析html/根据关键词拿到论坛帖子信息脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 以论坛宽带山》为例,需要根据给定的关键词,取得关于该关键词的所有帖子,包括人气数,发帖主题,回复数,发表人,发表时间,帖子链接,帖子详细文本内容等。

 

 

            详细代码如下:

 

Java代码

import java.util.ArrayList;  

import java.util.Hash@L_304_1@;  

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("p.mc").first()!=null){  

                Element contentsEle=doc.select("p.mc p").first();  

                contents=contentsEle.select("p").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方法中已有测试,网络畅通下可测试通过。但上面代码仅为完成功能,性能较差,项目中需重写或优化

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-jsoup解析html/根据关键词拿到论坛帖子信息全部内容,希望文章能够帮你解决javascript代码实例教程-jsoup解析html/根据关键词拿到论坛帖子信息所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。