【问题标题】:Asking a Server for a Table in Java用 Java 向服务器请求一张表
【发布时间】:2015-11-12 00:13:54
【问题描述】:

我想向这个服务器索要一张桌子,但我不知道该怎么做。基本上,我试图让 Eclipse 程序在运行程序时打印出这样的内容:

<tr><td>
 <pre>

                       <b>2011 Phases of the Moon</b>
                            Universal Time

        New Moon   First Quarter       Full Moon    Last Quarter   

         d  h  m         d  h  m         d  h  m         d  h  m

    Jan  4  9 03    Jan 12 11 31    Jan 19 21 21    Jan 26 12 57
    Feb  3  2 31    Feb 11  7 18    Feb 18  8 36    Feb 24 23 26
    Mar  4 20 46    Mar 12 23 45    Mar 19 18 10    Mar 26 12 07
    Apr  3 14 32    Apr 11 12 05    Apr 18  2 44    Apr 25  2 47
    May  3  6 51    May 10 20 33    May 17 11 09    May 24 18 52
    Jun  1 21 03    Jun  9  2 11    Jun 15 20 14    Jun 23 11 48
    Jul  1  8 54    Jul  8  6 29    Jul 15  6 40    Jul 23  5 02
    Jul 30 18 40    Aug  6 11 08    Aug 13 18 57    Aug 21 21 54
    Aug 29  3 04    Sep  4 17 39    Sep 12  9 27    Sep 20 13 39
    Sep 27 11 09    Oct  4  3 15    Oct 12  2 06    Oct 20  3 30
    Oct 26 19 56    Nov  2 16 38    Nov 10 20 16    Nov 18 15 09
    Nov 25  6 10    Dec  2  9 52    Dec 10 14 36    Dec 18  0 48
    Dec 24 18 06                                               
 </pre>
 </td></tr>

这是我目前所拥有的:

import java.util.Scanner;
import java.io.*;
import java.net.*;
/**
   This program shows the moon phase table for the given year
*/
public class MoonPhaseTable
{  
    public static void main(String[] args) throws IOException
    {  
        Scanner in = new Scanner(System.in); //read year number from user
        System.out.print("Please enter the year (e.g. 1977): ");
        int year = in.nextInt();

        // Build the URL string and open a URLConnection
        // Be sure to set the year on the URL string!!!
        URL moonphase = new URL("http://aa.usno.navy.mil/cgi-bin/aa_moonphases.pl?year=2015/");
        URLConnection mp = moonphase.openConnection();      

        // Get the connection's input stream, and make a Scanner for it
        BufferedReader r = new BufferedReader(new InputStreamReader(mp.getInputStream()));

        Scanner s = new Scanner(moonphase.openStream());

        String inputLine;

        boolean printingTable = false;
        while ((inputLine = r.readLine()) !=null) {
                 // Read input lines from the scanner into the String named line.

             if (printingTable) {     
                 // Check if the line contains the </table> end tag -- if seen, turn off printing
                 System.out.print("CONTAINS </table>");
                 s.close(); 
             }
             if (printingTable) {
                 // If inside the Table, print the line.
                 // Optionally clean up any unwanted tags, such as
                 // <pre>, <tr>, <td>, <b> before printing.
                 System.out.print(inputLine);

             }
             if (!printingTable) {
                 // Check if the line contains the <table ...> start tag -- if seen, turn on printing
                 System.out.print(inputLine);
             }
           }
        }

    }

现在,它打印了整个源代码。我只想要上面显示的代码。我知道我没有检查该行是否正确包含表格块,但我不确定如何执行此操作。如果有人有任何建议,将不胜感激。我想我必须向 aa.usno.navy.mil 服务器询问月球表,但又一次......我迷路了。

【问题讨论】:

    标签: java server html-table


    【解决方案1】:

    在循环内部,您需要根据收集的输入匹配来计算 printingTable

    这样的东西可能适用于计算 printingTable

    printingTable = inputLine.contains("table");
    

    这非常粗略,但在这种情况下可能工作得很好。

    如果 printingTabletruefalse 并且不更新 printingTable,现在代码正在打印行从它的初始值 false.

    另外,还有一些非常好的用于 Java 的 HTML5 解析库,例如 Validator.nu HTML Parser: https://about.validator.nu/htmlparser/

    它提供了 SAX、DOM 和其他解析模式。 DOM 可能是最好的,因为您可以轻松获取表格的整个子树并用它一一挑选元素。

    我用它来对工作中非常混乱的数据源进行网络挖掘,它使它更易于管理。我会请求允许发布我在 2011 年写的网络采矿指南。

    【讨论】:

    • @Britni.M 如果它适合你,请确保退回并接受这个答案;)
    • @Britni.M 很酷。我提供了一个(非常粗略的)建议的计算方法。
    • 啊,我现在看到你的编辑了。谢谢你。我已经在我的代码中实现了它,我还在玩它。是关闭打印 s.close(); 的方法吗?还是我应该使用其他东西..?
    • 我正在尝试关闭它以在 标记之后不打印任何内容,在 标记之前不打印任何内容
    猜你喜欢
    • 2012-12-20
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    相关资源
    最近更新 更多