Java 如何在新行上打印每一条信息
您好,代码运行良好,但我想知道是否有一种方法,我可以让它打印后,在一个新的行上的某个点刮的信息。对于这段代码,我希望每个公司的信息都打印在新的一行上。下面列出了代码设置为刮取的站点。非常感谢你的帮助和阅读。Java 如何在新行上打印每一条信息,java,jsoup,Java,Jsoup,您好,代码运行良好,但我想知道是否有一种方法,我可以让它打印后,在一个新的行上的某个点刮的信息。对于这段代码,我希望每个公司的信息都打印在新的一行上。下面列出了代码设置为刮取的站点。非常感谢你的帮助和阅读。 您的问题是将整个表体作为一个元素。您可以通过执行以下操作来解决此问题: Elements body = document.select("div#scr-res-table tbody tr"); 然后,下面的代码应该在单独的行上打印每个表行 for(Element p : bo
您的问题是将整个表体作为一个元素。您可以通过执行以下操作来解决此问题:
Elements body = document.select("div#scr-res-table tbody tr");
然后,下面的代码应该在单独的行上打印每个表行
for(Element p : body) {
System.out.println(p.text());
}
我根据你的代码创建了一个原型
public static void main(String[] args) {
System.out.println(new App().getGreeting());
print("running...");
Document document;
try {
document = Jsoup.connect("https://finance.yahoo.com/most-active").get();
Elements body = document.select("div#scr-res-table tbody");
String title = document.title();
print(" Title: " + title);
String table = printTable(body.first()); //<---remember to select the first found of the table body.
print(table);
} catch (IOException e) {
e.printStackTrace();
}
print("done");
}
public static void print(String string) {
System.out.println(string);
}
/**
* Extract data fields from each row <tr>....</tr>
*/
public static String printLine(Element row) {
//Each <td> cell has different content, you need to look at the
//html source and extract them individually, here I extract 3 of them as example
String symbol = row.select("td[aria-label='Symbol'] a").first().text();
String name = row.select("td[aria-label='Name']").first().text();
String volume = row.select("td[aria-label='Volume'] span").first().text();
return symbol + " " + name + " " + volume;
}
/**
* Take in a <tbody> tree, and print the every row inside as a string.
*/
public static String printTable(Element tableBody){
StringBuffer sb = new StringBuffer();
Elements allRows = tableBody.select("tbody").first().children();
for(Element row : allRows){
String rowString = printLine(row);
sb.append(rowString).append(System.lineSeparator());
}
return sb.toString();
}
您需要在以后添加自己的格式。如果您发布一个要拆分的文本示例,这会有所帮助。通用电气公司5.79-0.21-3.50%234.597M 108.097M 50.646B十亿美元/A:美国银行股份有限公司20.87-1.00-4.57%110.266M91.593M181.06B8.48:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::像这样的话,前两行将分开打印,而不是像一堆文本,我只是使用了:把队伍分成两半,这很有帮助。我对编码比较新,所以谢谢你的支持。它能回答你的问题吗?
public static void main(String[] args) {
System.out.println(new App().getGreeting());
print("running...");
Document document;
try {
document = Jsoup.connect("https://finance.yahoo.com/most-active").get();
Elements body = document.select("div#scr-res-table tbody");
String title = document.title();
print(" Title: " + title);
String table = printTable(body.first()); //<---remember to select the first found of the table body.
print(table);
} catch (IOException e) {
e.printStackTrace();
}
print("done");
}
public static void print(String string) {
System.out.println(string);
}
/**
* Extract data fields from each row <tr>....</tr>
*/
public static String printLine(Element row) {
//Each <td> cell has different content, you need to look at the
//html source and extract them individually, here I extract 3 of them as example
String symbol = row.select("td[aria-label='Symbol'] a").first().text();
String name = row.select("td[aria-label='Name']").first().text();
String volume = row.select("td[aria-label='Volume'] span").first().text();
return symbol + " " + name + " " + volume;
}
/**
* Take in a <tbody> tree, and print the every row inside as a string.
*/
public static String printTable(Element tableBody){
StringBuffer sb = new StringBuffer();
Elements allRows = tableBody.select("tbody").first().children();
for(Element row : allRows){
String rowString = printLine(row);
sb.append(rowString).append(System.lineSeparator());
}
return sb.toString();
}
Hello world.
running...
Title: Most Active Stocks Today - Yahoo Finance
GE General Electric Company 234.597M
BAC Bank of America Corporation 110.266M
F Ford Motor Company 97.974M
....
done