c 抓取网页数据(求大师教你如何用jsoup拆分获取到数据存放到数据库)
优采云 发布时间: 2022-01-16 17:21c 抓取网页数据(求大师教你如何用jsoup拆分获取到数据存放到数据库)
表中的数据需要从以下网址获取(即右边的台风数据)
目前的思路是使用正则表达式,指定路径,获取表格中的所有元素
代码显示如下:
public class CodeRegex {
/**
* 获取大陆台风页面table元素
* */
public String getHTML(){
try {
String HTML ="";
String ur = "http://gb.weather.gov.hk/wxinfo/currwx/tc_posc.htm"; // 获取远程网上的信息
URL MyURL = new URL(ur);
String str;
URLConnection con = MyURL.openConnection();
InputStreamReader ins = new InputStreamReader(con.getInputStream());
BufferedReader in = new BufferedReader(ins);
StringBuffer sb = new StringBuffer();
while ((str = in.readLine()) != null) {
sb.append(str);
}
in.close();
Pattern p = Pattern.compile("]*>.+?");
Matcher m = p.matcher(sb.toString());
while (m.find()) {
HTML = m.group();
System.out.println("html:"+HTML);
}
} catch (MalformedURLException mfURLe) {
System.out.println("MalformedURLException: " + mfURLe);
} catch (IOException ioe) {
System.out.println("IOException: " + ioe);
}
return getHTML();
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
System.out.println("content:");
TyphoonCNAction tcn = new TyphoonCNAction();
String content = tcn.getHTML();
System.out.println("content:"+content);
}
}
我不知道以后如何获取元素的值。
如何使用字符串拆分来获取数据并将其存储在数据库中
请回答源代码说明,如果做过类似项目,请到邮箱索取代码
据说用jsoup做会更容易,求高手帮忙源码