天气预报之抓取、解析、存入MYSQL数据库模块实现,不规范(代码)

该代码实现了一个天气预报的爬虫,抓取指定城市的天气信息,包括城市、日期、星期、温度、天气状况、风力等,并将数据解析后存储到MySQL数据库的weather_info表中。
摘要由CSDN通过智能技术生成
package com.zzk.cn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.sql.*;
import java.util.Enumeration;
import java.util.Properties;

import net.sf.json.JSONObject;

/**
 *
 * @author zhuzhengke
 * @version 1.0.0
 * 
 */


public class Json {

	static String info = "";

	/*主函数*/
	public static void main(String[] args) throws UnsupportedEncodingException {
		//抓取网页
		getInfo(101010100);
		//解析字段
		readJson();
		//insertMysql();
	}
	
	public class GetInfo {
		public void getInfo(int id) {
			
			String path = "http://m.weather.com.cn/data/"+id+".html";
			URL url;
			String inputline = "";
			InputStream input = null;
			InputStreamReader reader = null;
			BufferedReader buffer = null;
			
			try {
				url = new URL(path);
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();
				conn.setReadTimeout(10 * 1000);
				conn.setRequestMethod("GET");

				input = conn.getInputStream();
				reader = new InputStreamReader(input,"utf8");
				buffer = new BufferedReader(reader);

				while ((inputline = buffer.readLine()) != null) {
					info += inputline;
				}

			} catch (ProtocolException e) {
				e.printStackTrace();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally {
		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值