首页 > 解决方案 > 如何登录并继续在 JAVA 中进行抓取?

问题描述

我的问题是我必须能够在销售电子产品和设备的网站(本网站)上提取某些信息,例如每个产品的价格、数量和名称,但缺点是要查看这些信息,您必须先login,因此我必须登录然后提取所有信息。但是我当前的代码不允许我这样做,它从我那里提取信息,但它与我没有登录时获得的信息相同。

我的代码

Connection.Response loginForm = Jsoup.connect("https://www.elit.com.ar/productos/computadoras.html")
            .method(Connection.Method.GET)
            .execute();

            Document document = Jsoup.connect("https://www.elit.com.ar/productos/computadoras.html")
                    .data("username", username)
                    .data("password", password)
                    .cookies(loginForm.cookies())
                    .timeout(100000)
                    .post();
            System.out.println(document.getAllElements());

抱歉我的无知,我是Scraping的新手,主要是Java。谢谢你,我期待你的支持。

标签: javawebweb-scraping

解决方案


您需要先将数据发布到登录 url 并从那里使用 cookie。

由于我没有有效的凭据来测试,我不知道网站上的身份验证流程是什么。

但这里有一个要点

   // get login form
    Connection.Response loginForm = Jsoup.connect("https://www.elit.com.ar/clientes/login.html")
        .method(Connection.Method.GET)
        .execute();

    // POST login data
    Connection.Response loginResponse = Jsoup.connect("https://www.elit.com.ar/clientes/login.html")
        .data("username", username)
        .data("password", password)
        .cookies(loginForm.cookies())
        .timeout(100000)
        .post();

    // GET page
    Document document = Jsoup.connect("https://www.elit.com.ar/productos/computadoras.html")
        .method(Connection.Method.GET)
        .cookies(loginResponse.cookies())
        .timeout(100000)
        .execute();
    System.out.println(document.getAllElements());

我建议你看看这篇文章,了解身份验证流程在你正在抓取的网站上是如何工作的

http://joelmin.blogspot.com/2016/04/how-to-login-to-website-using-jsoup-java_4.html


推荐阅读