工作原因,需要提供一个外汇汇率的实时接口,各位大神有好爬的网站可以推荐一发,没有的话可以看小弟找的这个 传送门在这里: 传送门-斯通纳德
这是小菜鸡写的代码:
var url = require('url');
var superagent = require('superagent');
var cheerio = require('cheerio');
var eventproxy = require('eventproxy');
var targetUrl = 'http://quote.fx168.com/showration/';
superagent.get(targetUrl).end(function(err,res){
console.log(res.text);
var $ = cheerio.load(res.text);
$('.unnamed1').each(function (idx, element) {
console.log(element);
});
});
首先说下我的思路,哪不对各位大神指教一下哈 1 把目标网页源码爬下来 2 用cheerio解析 3 把上面两步做成接口,传送json数据
然后写cheerio的时候我整个人都斯巴达了
明显异步加载的,给你找到了,直接是类似jsonp的格式,爽YY了。
http://service.fx168.com/IDataPlatform/Quote/IQuoteConversionList.ashx?user=fx168&succ_callback=GetHbdhlSuccess
它这不是提供了jsonp接口了么, 在浏览器里面直接用就是了,为啥还要开服务端扒站? http://service.fx168.com/IDataPlatform/Quote/IQuoteConversionList.ashx?user=fx168&succ_callback=GetHbdhlSuccess
@meekr @feliving nodejs不太会 java写的解析 可以后面直接插入自己的数据库
==========class GetJsonFromUrl===========
package web.json;
import java.io.;
import java.net.;
public class GetJsonFromUrl {
protected String getJsonString(String urlPath) throws Exception {
URL url = new URL(urlPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
//对应的字符编码转换
Reader reader = new InputStreamReader(inputStream, “UTF-8”);
BufferedReader bufferedReader = new BufferedReader(reader);
String str = null;
StringBuffer sb = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
sb.append(str);
}
reader.close();
connection.disconnect();
return sb.toString();
}
}
=======class test ============ package web.json;
import net.sf.json.JSONArray; import net.sf.json.JSONObject;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
GetJsonFromUrl JsonBuilder = new GetJsonFromUrl();
String urlpath = "http://service.fx168.com/IDataPlatform/Quote/IQuoteConversionList.ashx?user=fx168&succ_callback=GetHbdhlSuccess";
String result = "";
try {
result = JsonBuilder.getJsonString(urlpath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//String resulttojson = result.substring(16,result.length()-1) ;
//System.out.println("result is " + result);
String resulttojson = result.substring(16,result.length()-1) ;
//System.out.println(resulttojson.length());
//System.out.println(result instanceof String);
JSONArray array = JSONArray.fromObject(resulttojson);
System.out.println(array.size());
for (int i = 0; i < array.size(); i++) {
JSONObject jsonObj = array.getJSONObject(i);
//System.out.println(jsonObj.get("CurrencyName"));
System.out.println(jsonObj.getJSONArray("Currency").get(0));
//System.out.println(jsonObj);
}
}
}
BTW:nodejs应该更简单思路就是 收到请求,去掉包装,然后字符串转json或者直接操作字符串,拆他的结构
@CarlosRen 那明显js的代码要简单的多。 一般jsonp去掉callback参数返回的就是json格式数据.
过滤美元对人民币
var superagent = require('superagent');
var targetUrl = 'http://service.fx168.com/IDataPlatform/Quote/IQuoteConversionList.ashx?user=fx168';
superagent
.get(targetUrl)
.end(function(err,res){
var data = JSON.parse(res.text);
var dollar = data[0];
dollar.Currency.forEach(function(item, index){
if(item.CurrencyConversionName =='人民币'){
console.log(dollar.CurrencyName + '==>' + item.CurrencyConversionName + ':' +item.CurrencyValue);
}
})
});