import com.google.common.collect.Lists; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.sele ...
import com.google.common.collect.Lists; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.util.Date; import java.util.List; /** * 網上爬取快遞100的快遞公司名稱和公司編碼 工具類 */ public class ExpressCodeUtil { public static void main(String[] args) { String url = "https://my.oschina.net/u/3291736/blog/843843"; try { Document doc = Jsoup.connect(url).get(); //獲取第一個表格 Element element = doc.select("table").first(); Elements els = element.select("tr");//一個tr是一個快遞公司編碼 List<ExpressCodeTbl> list = Lists.newArrayList();//ExpressCodeTbl是用來裝快遞公司名稱和公司編碼的實體類 for (int i = 0; i< els.size();i++) { ExpressCodeTbl code = new ExpressCodeTbl(); if (i != 0) { //每個快遞編碼處理 Elements ele = els.get(i).select("td"); code.setExpressName(ele.get(0).toString().replaceAll("<td>", "").replaceAll("</td>", "")); code.setExpressCode(ele.get(1).toString().replaceAll("<td>", "").replaceAll("</td>", "")); code.setAddTime(new Date()); list.add(code); } } //保存數據到資料庫 //ex.saveAll(list); for (ExpressCodeTbl code:list){ //輸出快遞公司名稱和編碼 System.out.println(code); } } catch (Exception e) { e.printStackTrace(); } } }