Using Java to pull data from a webpage?

后端 未结 3 1981
猫巷女王i
猫巷女王i 2020-11-28 21:29

I\'m attempting to make my first program in Java. The goal is to write a program that browses to a website and downloads a file for me. However, I don\'t know how to use Jav

3条回答
  •  清歌不尽
    2020-11-28 21:36

    Here's my solution using URL and try with resources phrase to catch the exceptions.

    /**
     * Created by mona on 5/27/16.
     */
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    public class ReadFromWeb {
        public static void readFromWeb(String webURL) throws IOException {
            URL url = new URL(webURL);
            InputStream is =  url.openStream();
            try( BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            }
            catch (MalformedURLException e) {
                e.printStackTrace();
                throw new MalformedURLException("URL is malformed!!");
            }
            catch (IOException e) {
                e.printStackTrace();
                throw new IOException();
            }
    
        }
        public static void main(String[] args) throws IOException {
            String url = "https://madison.craigslist.org/search/sub";
            readFromWeb(url);
        }
    
    }
    

    You could additionally save it to file based on your needs or parse it using XML or HTML libraries.

提交回复
热议问题