爬取页面的json数据
2016-01-13 14:20
645 查看
package pachong;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Spider {public static void main(String[] args) {
String filepath = "d:/125.html";
String url_str = "http://aqicn.org/forecast/beijing/cn/";
URL url = null;
try {
url = new URL(url_str);
} catch (MalformedURLException e) {
e.printStackTrace();
}
String charset = "utf-8";
int sec_cont = 1000;
try {
URLConnection url_con = url.openConnection();
url_con.setDoOutput(true);
url_con.setReadTimeout(10 * sec_cont);
url_con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
InputStream htm_in = url_con.getInputStream();
String htm_str = InputStream2String(htm_in,charset);
saveHtml(filepath,htm_str);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Method: saveHtml
* Description: save String to file
* @param filepath
* file path which need to be saved
* @param str
* string saved
*/
public static void saveHtml(String filepath, String str){
try {
/*@SuppressWarnings("resource")
FileWriter fw = new FileWriter(filepath);
fw.write(str);
fw.flush();*/
OutputStreamWriter outs = new OutputStreamWriter(new FileOutputStream(filepath, true), "utf-8");
outs.write(str);
System.out.print(str);
outs.close();
} catch (IOException e) {
System.out.println("Error at save html...");
e.printStackTrace();
}
}
/**
* Method: InputStream2String
* Description: make InputStream to String
* @param in_st
* inputstream which need to be converted
* @param charset
* encoder of value
* @throws IOException
* if an error occurred
*/
public static String InputStream2String(InputStream in_st,String charset) throws IOException{
BufferedReader buff = new BufferedReader(new InputStreamReader(in_st, charset));
StringBuffer res = new StringBuffer();
String line = "";
while((line = buff.readLine()) != null){
res.append(line);
}
return res.toString();
}
}
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Spider {public static void main(String[] args) {
String filepath = "d:/125.html";
String url_str = "http://aqicn.org/forecast/beijing/cn/";
URL url = null;
try {
url = new URL(url_str);
} catch (MalformedURLException e) {
e.printStackTrace();
}
String charset = "utf-8";
int sec_cont = 1000;
try {
URLConnection url_con = url.openConnection();
url_con.setDoOutput(true);
url_con.setReadTimeout(10 * sec_cont);
url_con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
InputStream htm_in = url_con.getInputStream();
String htm_str = InputStream2String(htm_in,charset);
saveHtml(filepath,htm_str);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Method: saveHtml
* Description: save String to file
* @param filepath
* file path which need to be saved
* @param str
* string saved
*/
public static void saveHtml(String filepath, String str){
try {
/*@SuppressWarnings("resource")
FileWriter fw = new FileWriter(filepath);
fw.write(str);
fw.flush();*/
OutputStreamWriter outs = new OutputStreamWriter(new FileOutputStream(filepath, true), "utf-8");
outs.write(str);
System.out.print(str);
outs.close();
} catch (IOException e) {
System.out.println("Error at save html...");
e.printStackTrace();
}
}
/**
* Method: InputStream2String
* Description: make InputStream to String
* @param in_st
* inputstream which need to be converted
* @param charset
* encoder of value
* @throws IOException
* if an error occurred
*/
public static String InputStream2String(InputStream in_st,String charset) throws IOException{
BufferedReader buff = new BufferedReader(new InputStreamReader(in_st, charset));
StringBuffer res = new StringBuffer();
String line = "";
while((line = buff.readLine()) != null){
res.append(line);
}
return res.toString();
}
}
相关文章推荐
- localStorage & JSON
- [转] 主流JS框架中DOMReady事件的实现
- Javascript 面向对象编程(一):封装
- JS 实现取整(二)
- 将获得到的json赋值到下拉框
- JSP利用freemarker生成基于word模板的word文档
- 学习 JavaScript 最难点之一 -- 理解prototype(原型)
- 封装常用的js(Base.js)——【01】理解库,获取节点,连缀,
- javascript Base类 包含基本的方法
- XML Json数据
- js调试工具Console命令详解
- jsonp跨域请求
- javascript笔记——jqGrid再次封装
- js 书写规范
- 加载本地html,js, css文 件
- SSH框架jsp迭代实体类,并获取关联对象的属性
- js倒计时
- js中数组的一些操作
- Javascript判断对象和字符串为空的优雅写法
- 数据导入