如何使用用户名和密码登录网站后抓取网站

时间:2015-01-23 12:43:15

标签: java jsoup web-crawler

我编写了一个使用键盘抓取网站的webcrawler但我想登录我指定的网站并按关键字过滤信息。如何实现这一点。我到目前为止发布了我的代码。

public class DB {

public Connection conn = null;

public DB() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/test";
        conn = DriverManager.getConnection(url, "root","root");
        System.out.println("conn built");
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

public ResultSet runSql(String sql) throws SQLException {
    Statement sta = conn.createStatement();
    return sta.executeQuery(sql);
}

public boolean runSql2(String sql) throws SQLException {
    Statement sta = conn.createStatement();
    return sta.execute(sql);
}

@Override
protected void finalize() throws Throwable {
    if (conn != null || !conn.isClosed()) {
        conn.close();
    }
}
}


public class Main {
public static DB db = new DB();

public static void main(String[] args) throws SQLException, IOException {
    db.runSql2("TRUNCATE Record;");
    processPage("http://m.naukri.com/login");
}

public static void processPage(String URL) throws SQLException, IOException{
    //check if the given URL is already in database;
    String sql = "select * from Record where URL = '"+URL+"'";
    ResultSet rs = db.runSql(sql);
    if(rs.next()){

    }else{
        //store the URL to database to avoid parsing again
        sql = "INSERT INTO  `test`.`Record` " + "(`URL`) VALUES " + "(?);";
        PreparedStatement stmt = db.conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        stmt.setString(1, URL);
        stmt.execute();

        //get useful information
        Connection.Response res = Jsoup.connect("http://www.naukri.com/").data("username","jeet.chatterjee.88@gmail.com","password","Letmein321")
                 .method(Method.POST)
                    .execute();  
        //http://m.naukri.com/login
        Map<String, String> loginCookies = res.cookies();
        Document doc = Jsoup.connect("http://m.naukri.com/login")
                  .cookies(loginCookies)
                  .get();

        if(doc.text().contains("")){
            System.out.println(URL);
        }

        //get all links and recursively call the processPage method
        Elements questions = doc.select("a[href]");
        for(Element link: questions){
            if(link.attr("abs:href").contains("naukri.com"))
                processPage(link.attr("abs:href"));
        }
    }
}
}

表格结构也是

 CREATE TABLE IF NOT EXISTS `Record` (
 `RecordID` INT(11) NOT NULL AUTO_INCREMENT,
 `URL` text NOT NULL,
  PRIMARY KEY (`RecordID`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

现在我想使用我的用户名和密码进行抓取,以便抓取工具可以动态登录到网站并根据关键字抓取信息。 让我们说我的用户名是lucifer&amp;密码是lucifer123

1 个答案:

答案 0 :(得分:3)

您的方法是无状态Web访问。通常适用于Web服务,而网站都是有状态的。 u验证一次,之后,他们使用存储在cookie中的会话密钥对您进行身份验证。所以这是必需的。你必须发送浏览器发送的参数。尝试使用firebug监控浏览器发送到网站的内容,并在代码中重现该内容

<强> - 更新 -

Jsoup.connect("url")
  .cookie("cookie-name", "cookie-value")
  .header("header-name", "header-value")
  .data("data-name","data-value");
你可以添加多个cookie |标题|数据。并且有从Map添加值的功能。

要找出必须设置的内容,在浏览器中添加fire bug,它们都有默认的开发人员控制台,可以使用F12启动。转到网址你想获取数据,只需将所有内容添加到你的jsoup请求中。 我从您的网站结果中添加了一些图片 capture

我用红色标记了重要部分。

您可以在代码中获取所需的Cookie,并将这些信息发送到网站并从中获取Cookie,并在获得response.cookies后,将这些Cookie附加到您发出的每个请求中;)

p.s:更改密码A.S.A.P