Web scraping 使用JSoup刮取电子邮件和链接

Web scraping 使用JSoup刮取电子邮件和链接,web-scraping,set,jsoup,Web Scraping,Set,Jsoup,我想使用JSoup提取网站的所有电子邮件地址和URL,并将其存储在哈希集中(这样就不会重复)。我正在尝试这样做,但我不确定我到底需要在select中输入什么,或者我是否做对了。代码如下: Document doc = Jsoup.connect(link).get(); Elements URLS = doc.select(""); Elements emails = doc.select(""); emailSet.add(emails.toString()); linksToVisit.a

我想使用JSoup提取网站的所有电子邮件地址和URL,并将其存储在哈希集中(这样就不会重复)。我正在尝试这样做,但我不确定我到底需要在select中输入什么,或者我是否做对了。代码如下:

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

Elements URLS = doc.select("");
Elements emails = doc.select("");
emailSet.add(emails.toString());
linksToVisit.add(URLS.toString());
这样做:


获取html文档:

Document doc = Jsoup.connect(link).get();
将电子邮件提取到哈希集中,使用正则表达式提取页面上的所有电子邮件地址:

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());
}
Pattern p=Pattern.compile(“[a-zA-Z0-9+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+”;
Matcher Matcher=p.Matcher(doc.text());
Set emails=newhashset();
while(matcher.find()){
emails.add(matcher.group());
}
提取链接:

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

Elements elements = doc.select("a[href]");
for (Element e : elements) {
    links.add(e.attr("href"));
}
Set links=newhashset();
Elements=doc.select(“a[href]”);
对于(元素e:元素){
links.add(e.attr(“href”);
}
在此填写完整的工作代码:


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

这是我的工作解决方案,它不仅可以在文本中搜索电子邮件,还可以在代码中搜索:

public Set<String> getEmailsByUrl(String url) {
    Document doc;
    Set<String> emailSet = new HashSet<>();

    try {
        doc = Jsoup.connect(url)
                .userAgent("Mozilla")
                .get();

        Pattern p = Pattern.compile("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+");
        Matcher matcher = p.matcher(doc.body().html());
        while (matcher.find()) {
            emailSet.add(matcher.group());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return emailSet;
}
public Set getEmailsByUrl(字符串url){
文件文件;
Set emailSet=newhashset();
试一试{
doc=Jsoup.connect(url)
.userAgent(“Mozilla”)
.get();
模式p=模式编译(“[a-zA-Z0-9+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-]+”;
Matcher Matcher=p.Matcher(doc.body().html());
while(matcher.find()){
emailSet.add(matcher.group());
}
}捕获(IOE异常){
e、 printStackTrace();
}
返回电子邮件集;
}