如何登录本网站并提取所需数据?

问题描述

我正在尝试通过以下链接从本网站(“您的价格”)中检索部件的自定义用户数据:“https://www.rhsparts.com/Item/11-309/American-Panel/spring -kit-brushed-chrome”。

我已经尝试了几个小时来使用不同的代码变体来实现这一目标。这是我当前的代码

    String url = "https://www.rhsparts.com/";
    String login = "loginUsername";
    String pass = "loginPassword";
    String agent = "Mozilla/5.0";
    Connection.Response res = Jsoup.connect(url).userAgent(agent).method(Connection.Method.GET).execute();

    Map<String,String> cookies = new HashMap<String,String>();
    Map<String,String> dataMap = new HashMap<String,String>();

    Document resdoc = res.parse();

    cookies.putAll(res.cookies());

    dataMap.put("user",login);
    dataMap.put("pass",pass);
    dataMap.put("attemptlogin",resdoc.select("input[name=attemptlogin]").attr("value"));
    // check value
    System.out.println(resdoc.select("input[name=attemptlogin]").attr("value"));

    Connection.Response doc = Jsoup.connect(url).userAgent(agent).cookies(cookies).data(dataMap).method(Connection.Method.POST).execute();

    Document doc2 = Jsoup.connect("https://www.rhsparts.com/Item/11-309/American-Panel/spring-kit-brushed-chrome").cookies(doc.cookies()).userAgent(agent).headers(doc.headers()).get();

    Elements e = doc2.select("table.price_table");
    System.out.println(e.text());

我做错了什么?我对 Jsoup 相当陌生。 我知道将 cookie 用于其他网站页面将使我保持登录状态,但我无法成功登录。提供的链接只是来自 RHS 网站的示例项目。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)