使用JSoup来抓取电子邮件和链接

Jon*_*han 3 set web-scraping jsoup

我想使用JSoup来提取网站的所有电子邮件地址和URL,并将其存储在一个哈希集中(因此不会重复).我试图这样做,但我不确定我需要在选择中输入什么,或者我是否正确行事.这是代码:

Document doc = Jsoup.connect(link).get();

Elements URLS = doc.select("");
Elements emails = doc.select("");
emailSet.add(emails.toString());
linksToVisit.add(URLS.toString());
Run Code Online (Sandbox Code Playgroud)

Jon*_*ica 7

这样做:


获取html文档:

Document doc = Jsoup.connect(link).get();
Run Code Online (Sandbox Code Playgroud)

将电子邮件提取到HashSet中,使用正则表达式提取页面上的所有电子邮件地址:

Pattern p = Pattern.compile("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+");
Matcher matcher = p.matcher(doc.text());
Set<String> emails = new HashSet<String>();
while (matcher.find()) {
   emails.add(matcher.group());
}
Run Code Online (Sandbox Code Playgroud)

提取链接:

Set<String> links = new HashSet<String>();

Elements elements = doc.select("a[href]");
for (Element e : elements) {
    links.add(e.attr("href"));
}
Run Code Online (Sandbox Code Playgroud)

完整且有效的代码在这里:https://gist.github.com/JonasCz/a3b81def26ecc047ceb5

现在不要成为垃圾邮件发送者!