java - 如何登录并继续在 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。谢谢你,我期待你的支持。
解决方案
您需要先将数据发布到登录 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
推荐阅读
- dart - 如何提高颤振性能?
- database - 一个在 PostgreSQL 中自动恢复数据库的脚本
- sql - TERADATA:更新行时生成不需要的空格
- firebase - 按最新添加到数据库的顺序
- c++ - 有没有办法以编程方式从 AppStore 更新应用程序?
- html - 垂直对齐 div 和图像
- ruby-on-rails - Rails:如何从包含的关注点覆盖或将给定范围设为私有?
- c++ - Qt:如何检查键盘焦点是否在 Qt 应用程序主窗口上?
- visual-studio - 如何修复“访问控制列表 (ACL) 结构无效”
- android - 如何在Android上为整个应用制作一个底部App Bar?