Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Webcrawler提取电子邮件_Java_Email_Web Crawler - Fatal编程技术网

Java Webcrawler提取电子邮件

Java Webcrawler提取电子邮件,java,email,web-crawler,Java,Email,Web Crawler,我想写一个网络爬虫,从一个页面开始,然后到该页面上的每个链接寻找电子邮件地址。这是我到目前为止所做的,但除了从一个网页转到另一个网页之外,它什么也没做 `package com.netinstructions.crawler; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; public class WebCrawler { pri

我想写一个网络爬虫,从一个页面开始,然后到该页面上的每个链接寻找电子邮件地址。这是我到目前为止所做的,但除了从一个网页转到另一个网页之外,它什么也没做

`package com.netinstructions.crawler;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class WebCrawler {

    private static final int MAX_PAGES_TO_SEARCH = 26;
    private Set<String> pagesVisited = new HashSet<String>();
    private List<String> pagesToVisit = new LinkedList<String>();
    private List<String> emails = new LinkedList<>();

private String nextUrl()
{
    String nextUrl;
    do
    {
        nextUrl = this.pagesToVisit.remove(0);
    } while(this.pagesVisited.contains(nextUrl));
    this.pagesVisited.add(nextUrl);
    return nextUrl;
}

public void search(String url, String searchWord)
{
    while(this.pagesVisited.size() < MAX_PAGES_TO_SEARCH)
    {
        String currentUrl;
        SpiderLeg leg = new SpiderLeg();
        if(this.pagesToVisit.isEmpty())
        {
            currentUrl = url;
            this.pagesVisited.add(url);
        }
        else
        {
            currentUrl = this.nextUrl();
        }
        leg.crawl(currentUrl); // Lots of stuff happening here. Look at the crawl method in
        // SpiderLeg
        leg.searchForWord(currentUrl, emails);
        this.pagesToVisit.addAll(leg.getLinks());
        this.pagesToVisit.addAll(leg.getLinks());
    }
    System.out.println(emails.toString());
    //System.out.println(String.format("**Done** Visited %s web page(s)", this.pagesVisited.size()));
}
}
`package com.netinstructions.crawler;
导入java.util.HashSet;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Set;
公共类网络爬虫器{
私有静态最终int MAX_PAGES_TO_SEARCH=26;
private Set pagesVisited=new HashSet();
private List pagesToVisit=new LinkedList();
私人列表电子邮件=新建LinkedList();
私有字符串nextur()
{
弦下弦;
做
{
nextUrl=this.pagesToVisit.remove(0);
}而(this.pagesVisited.contains(nextur));
此.pagesVisited.add(下一页);
下个月返回;
}
公共无效搜索(字符串url、字符串searchWord)
{
while(this.pagesVisited.size()
这是我的蜘蛛腿课

package com.netinstructions.crawler;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

public class SpiderLeg
{
// We'll use a fake USER_AGENT so the web server thinks the robot is a normal web browser.
private static final String USER_AGENT =
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
private List<String> links = new LinkedList<String>();
private Document htmlDocument;


/**
 * This performs all the work. It makes an HTTP request, checks the response, and then gathers
 * up all the links on the page. Perform a searchForWord after the successful crawl
 *
 * @param url
 *            - The URL to visit
 * @return whether or not the crawl was successful
 */
public boolean crawl(String url)
{
    try
    {
        Connection connection = Jsoup.connect(url).userAgent(USER_AGENT);
        Document htmlDocument = connection.get();
        this.htmlDocument = htmlDocument;
        if(connection.response().statusCode() == 200) // 200 is the HTTP OK status code
        // indicating that everything is great.
        {
            System.out.println("\n**Visiting** Received web page at " + url);
        }
        if(!connection.response().contentType().contains("text/html"))
        {
            System.out.println("**Failure** Retrieved something other than HTML");
            return false;
        }
        Elements linksOnPage = htmlDocument.select("a[href]");
        //System.out.println("Found (" + linksOnPage.size() + ") links");
        for(Element link : linksOnPage)
        {
            this.links.add(link.absUrl("href"));
        }
        return true;
    }
    catch(IOException ioe)
    {
        // We were not successful in our HTTP request
        return false;
    }
}


/**
 * Performs a search on the body of on the HTML document that is retrieved. This method should
 * only be called after a successful crawl.
 *
 * @param searchWord
 *            - The word or string to look for
 * @return whether or not the word was found
 */
public void searchForWord(String searchWord, List<String> emails)
{

    if(this.htmlDocument == null)
    {
        System.out.println("ERROR! Call crawl() before performing analysis on the document");
        //return false;
    }
    Pattern pattern =
            Pattern.compile("\"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\\\.[A-Z]{2,6}$\", Pattern.CASE_INSENSITIVE");

    Matcher matchs = pattern.matcher(searchWord);

    while (matchs.find()) {
        System.out.println(matchs.group());
    }
}


public List<String> getLinks()
{
    return this.links;
}

}
Pattern.compile("\"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\\\.[A-Z]{2,6}$\", Pattern.CASE_INSENSITIVE");
package com.netinstructions.crawler;
导入java.io.IOException;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
导入org.jsoup.Connection;
导入org.jsoup.jsoup;
导入org.jsoup.nodes.Document;
导入org.jsoup.nodes.Element;
导入org.jsoup.select.Elements;
公共级蜘蛛腿
{
//我们将使用一个假的用户代理,这样web服务器就会认为机器人是一个普通的web浏览器。
私有静态最终字符串用户\u代理=
“Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/535.1(KHTML,类似Gecko)Chrome/13.0.782.112 Safari/535.1”;
私有列表链接=新的LinkedList();
私人文件;
/**
*这将执行所有工作。它发出HTTP请求,检查响应,然后收集数据
*打开页面上的所有链接。成功爬网后执行searchForWord
*
*@param-url
*-要访问的URL
*@return爬网是否成功
*/
公共布尔爬网(字符串url)
{
尝试
{
Connection-Connection=Jsoup.connect(url).userAgent(USER\u-AGENT);
文档htmlDocument=connection.get();
this.htmlDocument=htmlDocument;
if(connection.response().statusCode()==200)//200是HTTP OK状态码
//表明一切都很好。
{
System.out.println(“\n**访问**在“+url”处收到的网页);
}
如果(!connection.response().contentType()包含(“text/html”))
{
System.out.println(“**失败**检索到HTML以外的内容”);
返回false;
}
Elements linksOnPage=htmlDocument.select(“a[href]”);
//System.out.println(“找到(“+linksOnPage.size()+”)链接”);
对于(元素链接:linksOnPage)
{
this.links.add(link.absUrl(“href”);
}
返回true;
}
捕获(ioe异常ioe)
{
//我们的HTTP请求没有成功
返回false;
}
}
/**
*对检索到的HTML文档的正文执行搜索。此方法应
*只能在成功爬网后调用。
*
*@param searchWord
*-要查找的单词或字符串
*@return是否找到该单词
*/
public void searchForWord(字符串searchWord,列表电子邮件)
{
if(this.htmlDocument==null)
{
System.out.println(“错误!在对文档执行分析之前调用crawl());
//返回false;
}
图案=
Pattern.compile(“\”^[A-Z0-9.\%+-]+@[A-Z0-9.-]+\\\.[A-Z]{2,6}$\”,Pattern.不区分大小写);
Matcher matchs=pattern.Matcher(searchWord);
while(matchs.find()){
System.out.println(matchs.group());
}
}
公共列表getLinks()
{
返回此链接;
}
}
我的网络爬虫是从另一个来源获取的,我做了一些更改。我添加了一个列表来保存电子邮件,并将它们全部返回给我。我想我在接收电子邮件并将其放入列表的方式上出错了,但我不确定如何修复它

蜘蛛腿类

package com.netinstructions.crawler;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

public class SpiderLeg
{
// We'll use a fake USER_AGENT so the web server thinks the robot is a normal web browser.
private static final String USER_AGENT =
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
private List<String> links = new LinkedList<String>();
private Document htmlDocument;


/**
 * This performs all the work. It makes an HTTP request, checks the response, and then gathers
 * up all the links on the page. Perform a searchForWord after the successful crawl
 *
 * @param url
 *            - The URL to visit
 * @return whether or not the crawl was successful
 */
public boolean crawl(String url)
{
    try
    {
        Connection connection = Jsoup.connect(url).userAgent(USER_AGENT);
        Document htmlDocument = connection.get();
        this.htmlDocument = htmlDocument;
        if(connection.response().statusCode() == 200) // 200 is the HTTP OK status code
        // indicating that everything is great.
        {
            System.out.println("\n**Visiting** Received web page at " + url);
        }
        if(!connection.response().contentType().contains("text/html"))
        {
            System.out.println("**Failure** Retrieved something other than HTML");
            return false;
        }
        Elements linksOnPage = htmlDocument.select("a[href]");
        //System.out.println("Found (" + linksOnPage.size() + ") links");
        for(Element link : linksOnPage)
        {
            this.links.add(link.absUrl("href"));
        }
        return true;
    }
    catch(IOException ioe)
    {
        // We were not successful in our HTTP request
        return false;
    }
}


/**
 * Performs a search on the body of on the HTML document that is retrieved. This method should
 * only be called after a successful crawl.
 *
 * @param searchWord
 *            - The word or string to look for
 * @return whether or not the word was found
 */
public void searchForWord(String searchWord, List<String> emails)
{

    if(this.htmlDocument == null)
    {
        System.out.println("ERROR! Call crawl() before performing analysis on the document");
        //return false;
    }
    Pattern pattern =
            Pattern.compile("\"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\\\.[A-Z]{2,6}$\", Pattern.CASE_INSENSITIVE");

    Matcher matchs = pattern.matcher(searchWord);

    while (matchs.find()) {
        System.out.println(matchs.group());
    }
}


public List<String> getLinks()
{
    return this.links;
}

}
Pattern.compile("\"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\\\.[A-Z]{2,6}$\", Pattern.CASE_INSENSITIVE");
这不应该是

Pattern.compile("[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}", Pattern.CASE_INSENSITIVE);
电子邮件中没有添加任何内容,因此您需要将找到的电子邮件推送到列表中。其次,您可能希望解析HTML文档,而不是页面的URL。由于该方法现在不返回任何内容,因此需要扩展if语句以避免空指针。
searchForWord
方法应为:

public void searchForWord(String searchWord, List<String> emails)
{

    if(this.htmlDocument == null)
    {
        System.out.println("ERROR! Call crawl() before performing analysis on the document");
    } else
    {
        String input = this.htmlDocument.toString();

        Pattern pattern =
                Pattern.compile("[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}", Pattern.CASE_INSENSITIVE);

        Matcher matchs = pattern.matcher(input);

        while (matchs.find()) {
            emails.push(matchs.group());
        }
    }
}
public void searchForWord(字符串searchWord,列出电子邮件)
{
if(this.htmlDocument==null)
{
System.out.println(“错误!在对文档执行分析之前调用crawl());
}否则
{
字符串输入=this.htmlDocument.toString();
图案=
Pattern.compile(“[A-Z0-9.\%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}”,Pattern.不区分大小写);
Matcher Matcher=pattern.Matcher(输入);
while(matchs.find()){
emails.push(matchs.group());
}
}
}

我运行了你的代码,但它仍然没有达到我希望的效果。列表中仍然充满了空插槽。我确实有一个关于正则表达式的问题。如果有人在他们的网站上“发电子邮件给我”example@gmail.com.“它还会找到电子邮件吗(我问,因为它以