简体   繁体   English

Java-登录网站以获取数据

[英]Java - Logging into a website in order to retrieve data

What I am attempting to do is: Login to a website in order to retrieve data that can only be accessed while logged on. 我正在尝试做的是:登录网站以检索只能在登录时访问的数据。

The website I need to login to is https://www.indemed.com . 我需要登录的网站是https://www.indemed.com

I think that this is a two part program, part 1 being logging in, while part 2 is getting the information. 我认为这是一个分为两部分的程序,第1部分正在登录,而第2部分正在获取信息。 When I run the login part of my program and then attempt to manually log in it says my account is in use, which I take to mean it is correctly logging in. 当我运行程序的登录部分,然后尝试手动登录时,表明我的帐户正在使用,这意味着它已正确登录。

However when I try to get the price it is not there (if not logged in prices will not show up, but everything else will be there). 但是,当我尝试获取价格时,价格不存在(如果未登录,价格将不会显示,但其他所有信息都将存在)。

My questions are: Is there a problem with how I am combining my logging method and my retrieving method? 我的问题是:我如何结合日志记录方法和检索方法是否存在问题? Is the problem just with my logging method? 问题仅在于我的日志记录方法吗? Is the problem with just my retrieving method? 我的检索方法是否有问题? Why doesn't this work? 为什么不起作用? Most importantly, how can I fix this? 最重要的是,我该如何解决?

Here is what I have attempted so far: 到目前为止,这是我尝试过的事情:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class IndependenceMedical {
    public IndependenceMedical(){
        login();
    }
    private void login() {
        URL URLObj;
        URLConnection connect;
        try {
            // Establish a URL and open a connection to it. Set it to output mode.
            URLObj = new URL("https://www.indemed.com/Action/Login/LoginAction.cfm?Refer=/index.cfm");
            connect = URLObj.openConnection();
            System.out.println(connect.toString());
            connect.setDoOutput(true);

            // Create a buffered writer to the URLConnection's output stream and write our forms parameters.
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));

            writer.write("AccountNumber=12345&UserName=myUserName&Password=myPassword&Login=Login");
            writer.close();
            // Now establish a buffered reader to read the URLConnection's input stream.
            BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
            String lineRead = "";
            // Read all available lines of data from the URL and print them to screen.
            while ((lineRead = reader.readLine()) != null) {
                System.out.println(lineRead);
            }
            reader.close();
        }
        catch (MalformedURLException ex) {
            System.out.println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
            System.exit(1);
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage() + "\nAn exception occurred.");
            System.exit(1);
        }
    }

    public Document getDoc(String itemNumber){
        try {
            return Jsoup.connect("https://www.indemed.com/Catalog/SearchResults.cfm?source=advancedSearch&psku=" + itemNumber + "&keyword=&PHCPCS=&PClassID=&ManufacturerID=&Search.x=41&Search.y=9").get();
        } 
        catch (IOException e) {}
            return null;
        }
    public String getPrice(Document doc){
        try{
            Elements stuff = doc.select("#tr_51187955");
            stuff = stuff.select("div.product-price");
            String newStuff = stuff.toString();
            newStuff = newStuff.substring(newStuff.indexOf("$")); // throws exception because "$" is not in the String.
            newStuff = newStuff.substring(0, newStuff.indexOf(" "));
            return newStuff;
        }
        catch (Exception arg0){
            return "";
        }
    }



    public static void main(String[] args){
        IndependenceMedical test = new IndependenceMedical();
        Document doc = test.getDoc("187955");
        System.out.println("\n\n\n\n\n\n\n\n\n\n"); //to separate the return lines
        System.out.println(test.getPrice(doc));
    }
}

Due to character restrictions and the fact that I don't know which parts are important, I can't show the output. 由于角色的限制以及我不知道哪个部分很重要的事实,我无法显示输出。 However if requested I will try to provide all the requested output. 但是,如果有要求,我将尝试提供所有要求的输出。

Sorry for being so wordy I'm just trying to make sure the question is clear. 抱歉这么罗word,我只是想确保问题清楚。

Lastly I have thoroughly looked through other login questions and although there are examples of how to login, I can't seem to find how to do anything after logging in (i'm sure someone has talked about it, but I haven't been able to find it). 最后,我彻底浏览了其他登录问题,尽管有一些登录示例,但登录后我似乎找不到任何方法(我敢肯定有人在谈论它,但是我一直没有能够找到它)。

Thanks in advance to anyone that can help me with this. 在此先感谢任何可以帮助我的人。

EDIT: 编辑:

Although this question is similar to Parse HTML source after login with Java 尽管此问题类似于使用Java登录后解析HTML源代码

I'm not parsing the redirected page, I need access to all pages this grants access to. 我没有解析重定向的页面,我需要访问所有授予访问权限的页面。

Jsoup provides the methods for login mechanisms. Jsoup提供了登录机制的方法。 Try the below, after you've filled the username, password and account number. 填写用户名,密码和帐号后,请尝试以下操作。

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class IndependenceMedical {

    private Map<String, String> loginCookies;

    public IndependenceMedical() {
        login();
    }

    private void login() {
        try {
            Connection.Response res = Jsoup.connect("https://www.indemed.com/Action/Login/LoginAction.cfm?refer=MyAccount&qs=")
                    .data("UserName",       "myUserName")
                    .data("Password",       "myPassword")
                    .data("AccountNumber",  "myAccountNumber")
                    .method(Method.POST)
                    .execute();

            loginCookies = res.cookies();
        } catch (MalformedURLException ex) {
            System.out.println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
            System.exit(1);
        } catch (Exception ex) {
            System.out.println(ex.getMessage() + "\nAn exception occurred.");
            System.exit(1);
        }
    }

    public Document getDoc(String itemNumber){
        try {
            return Jsoup.connect("https://www.indemed.com/Catalog/SearchResults.cfm?source=advancedSearch&psku=" + itemNumber + "&keyword=&PHCPCS=&PClassID=&ManufacturerID=&Search.x=41&Search.y=9")
                        .cookies(loginCookies)
                        .get();

        } catch (IOException e) {}

        return null;
    }

    public String getPrice(Document doc){
        try {
            Elements stuff = doc.select("#tr_51187955");
            stuff = stuff.select("div.product-price");
            String newStuff = stuff.toString();
            newStuff = newStuff.substring(newStuff.indexOf("$")); // throws exception because "$" is not in the String.
            newStuff = newStuff.substring(0, newStuff.indexOf(" "));
            return newStuff;
        } catch (Exception arg0) {
            return "";
        }
    }

    public static void main(String[] args){
        IndependenceMedical test = new IndependenceMedical();
        Document doc = test.getDoc("187955");
        System.out.println("\n\n\n\n\n\n\n\n\n\n"); //to separate the return lines
        System.out.println(test.getPrice(doc));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM