爬取西安电子科技大学教师主页的所所有教师邮箱 (使用java的jsoup)

1. 简介

为了爬取西电的教师主页信息,我选取了旧版教师主页旧版教师主页的较新版教师主页的比较稳定,并且老师更新的信息更为详细了。

此次利用的是Jsoup:  Jsoup 中文文档

2.基本思路

通过主页拿到学院URL-->各学院的老师URL -->所有老师页面的邮箱信息

3.代码


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import static java.lang.Thread.sleep;

public class JsoupTestTitle {

    public  static List<String> school = new ArrayList<String>();
    public  static List<String> zhuye = new ArrayList<String>();
    public  static Set<String> email = new TreeSet<>();

    public static void main(String[] args) throws Exception {
        getSchool();
        getZhuye();
        getEmail();
    }


    //爬取全部学院的url
    public static void getSchool() {

        String url = "https://web.xidian.edu.cn/showcollege.php?col_num=1";
        Document doc = null;
        try {
            doc = Jsoup.connect(url).get();
            Elements listDiv = doc.getElementsByAttributeValue("class", "right_container");
            for (Element element : listDiv) {
                Elements texts = element.getElementsByTag("a");
                for (Element text : texts) {

                    // 取所有文本
                    //String ptext = text.text();

                    String ptext = text.attr("href");


                    if("s".equals(ptext.substring(0,1))){
                        school.add("https://web.xidian.edu.cn/" + ptext);
                    }

                    //if(!ptext.equals(""))
                    // System.out.println("https://web.xidian.edu.cn/" + ptext);

                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    //按照学院爬取教师的主页url
    public static void getZhuye() {

        for(String ss : school){

            String url = ss;
            //for(String ss : school)
            Document doc = null;
            try {
                doc = Jsoup.connect(url).get();
                Elements listDiv = doc.getElementsByAttributeValue("class", "left_item");

                for (Element element : listDiv) {
                    Elements texts = element.getElementsByTag("a");
                    for (Element text : texts) {

                        // 取所有文本
                        //String ptext = text.text();

                        String ptext = text.attr("href");
                        String ptitle = text.attr("title");
                        zhuye.add("https://web.xidian.edu.cn/" + ptext + "+" + ptitle);
                        //if(!ptext.equals(""))
                        // System.out.println("https://web.xidian.edu.cn/" + ptext);

                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }


    //按照教师主页爬取教师的邮箱
    public static void getEmail() throws InterruptedException {

        for(String zz : zhuye) {
            int index = zz.indexOf("+");


            String url = zz.substring(0,index);
            String name = zz.substring(index+1,zz.length());
            sleep(1000);

            //String url = "https://web.xidian.edu.cn/baoliang/";
            Document doc = null;
            try {
                doc = Jsoup.connect(url).get();
                Elements listDiv = doc.getElementsByAttributeValue("class", "nr");
                sleep(1000);
                for (Element element : listDiv) {
                    String Content = element.text();
                    //正则表达式判断邮箱
                    String patternStr = "[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+";
                    Pattern pattern = Pattern.compile(patternStr);
                    Matcher matcher = pattern.matcher(Content);
                    //如果主页含有邮箱
                    if (matcher.find()) {

                        String teacherEmail = name + ":" + matcher.group();
                        if(!email.contains(teacherEmail)){
                            //email.add(teacherEmail);
                            System.out.println(teacherEmail);
                        }

                    }
                }
            } catch (IOException | InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

4. 运行结果:

5. 一些注意事项:

  • 西电旧版教师主页很不规范,有的是  电子邮件: [email protected]  有的是 电子邮箱: [email protected]  有的直接是[email protected]; 邮箱的位置也不规范,有的在简介中,有的在左边栏,有的在右边栏。所以我最终采用了正则表达式直接匹配邮箱的方式。
  • 采用了sleep(),因为一开始爬的太快被暂时封了ip好像......Jsoup.connect 带user-agent 更容易被封ip 好像.......
  • 这个运行之后会抛出 java.net.SocketTimeoutException: connect timed out 的异常,在 Jsoup.connect 中设置 timeout 也没有用,有待优化
  • 最终的结果链接见: https://share.weiyun.com/5Hqwqhv
发布了108 篇原创文章 · 获赞 48 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/larry1648637120/article/details/103354591