gpt4 book ai didi

java - 如何登录并进行JAVA抓取?

转载 作者:行者123 更新时间:2023-12-02 09:35:00 25 4
gpt4 key购买 nike

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

我的代码

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());

抱歉我的无知,我是抓取的新手,主要是Java。谢谢您,期待您的支持。

最佳答案

您需要首先将数据发布到登录网址并使用那里的 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

关于java - 如何登录并进行JAVA抓取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57596303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com