[英]Java- Obtaining bold text in HTML
我的任務是從定期更新的在線表格中獲取定價信息。 具體來說,以粗體列出的項目表示價格最近已更新的項目。 我主要負責這些工作,並將這些項目的信息傳輸到公司的數據庫中。 但是,我對在Java中使用HTML表有些陌生,並且不確定如何精確過濾粗體項目。 我確實注意到它們周圍有“ strong”標簽; 我只是不確定如何利用它。 我已經對谷歌搜索進行了廣泛嘗試,但是我發現的幾乎所有問題都與寫入HTML表而不是從中讀取有關。 我發現與我的問題有關的是使用Jsoup或jQuery的許多建議。 這些對於我想做的事情是否必要(或至少使其變得更加簡單)?
這是我到目前為止為其中一個表提供的代碼(如果它有幫助的話),盡管它當前所做的只是顯示整個表的HTML。
package cmtabacosPrecios;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class CeutaYMelilla_Cigarillos {
public static void main(String[] args) throws Exception {
// Run the getHTML method
try {
getHTML("http://www.cmtabacos.es/wwwcmt/listaPrecios.php?&zona=cm&labor=cillos&mostrar_codigo=S");
}
catch (Exception ex) {
System.out.println("Error. Exception has occured.");
}
}
// The getHTML method
public static String getHTML(String URLToRead) throws Exception {
URL cmtabacosPrecios = new URL(URLToRead);
BufferedReader in = new BufferedReader(
new InputStreamReader(cmtabacosPrecios.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
// Return HTML as a String
return inputLine;
}
}
下面是使用jsoup從表中檢索數據的示例代碼。 只需對其進行修改即可獲取對您很重要的數據。 您還可以在項目網站上了解有關jsoup
更多信息,它非常簡單而且功能強大。
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect("http://www.cmtabacos.es/wwwcmt/listaPrecios.php?&zona=cm&labor=cillos&mostrar_codigo=S").get();
Elements tableRows = doc.select("tr");
for (Element row : tableRows) {
Elements marca = row.getElementsByClass("marca");
Elements pvpExp = row.getElementsByClass("pvp_exp");
Elements pvpPvr = row.getElementsByClass("pvp_pvr");
if (!marca.isEmpty() && !pvpExp.isEmpty() && !pvpPvr.isEmpty()) {
System.out.println(marca.get(0).text());
System.out.println(pvpExp.get(0).text());
System.out.println(pvpPvr.get(0).text());
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.