• 寒假学习进度-8(热词爬取)


    package hotword;
    
    import java.io.IOException;
    
    import org.jsoup.Connection;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    import dao.Dao;
    
    public class Baidu1 {
        public static void main(String[] args)
        {
            lianjie("https://baike.baidu.com/item/%E6%89%8B%E6%9C%BA/6342");
            
        }
        public static void lianjie(String address)
        {
            String explanation="";
            String title="";
            String url="";
            try {
                Document document=Jsoup.connect(address).get();    
                Elements links=document.getElementsByClass("para");
                Elements reault=links.select("a[href]");
                for(Element link:reault)
                {
                    String linkHref=link.attr("href");
                    //System.out.println(linkHref);
                    String title1=link.text();
                    //System.out.println(title1);
                    title=timu("https://baike.baidu.com"+linkHref);
                    explanation=ex("https://baike.baidu.com"+linkHref);
                    url="https://baike.baidu.com"+linkHref;
                    System.out.println(title);
                    System.out.println(explanation);
                    Dao dao=new Dao();
                    try {
                        //dao.Hotword(title, explanation, url);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        public static String timu(String address)
        {
            String name="";
            try {
                Document document=Jsoup.connect(address).get();
                Elements ProjectName=document.getElementsByClass("lemmaWgt-lemmaTitle-title");
                Elements h1=ProjectName.select("h1");
                Elements h2=ProjectName.select("h2");
                name=h1.text()+h2.text();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return name;
        }
        public static String ex(String address)
        {
            String name="";
            try {
                Document document=Jsoup.connect(address).get();
                Elements ProjectName=document.getElementsByClass("lemma-summary");
                name=ProjectName.text();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return name;
        }
    
    }

    截图:

  • 相关阅读:
    【Django】Django 定时任务实现(django-crontab+command)
    【Django】Django web项目部署(Nginx+uwsgi)
    【python】pandas & matplotlib 数据处理 绘制曲面图
    Struts2的简单使用
    Servlet,jsp,JSP技术 ,JSP编程
    Servlet、JSP中页面跳转的方式
    html/京东项目/京东网页高仿/js/jq/css/java web/
    Java ResourceBundle类的使用
    sql语句executeQuery和executeUpdate之间的区别
    jdbc/DAO模式
  • 原文地址:https://www.cnblogs.com/liujinxin123/p/12263238.html
Copyright © 2020-2023  润新知