您好,登錄后才能下訂單哦!
隨著國(guó)家發(fā)展,中國(guó)很多城市的空氣質(zhì)量其實(shí)并不好,國(guó)家氣象局會(huì)有實(shí)時(shí)統(tǒng)計(jì),但是要去寫(xiě)爬蟲(chóng)爬取是十分麻煩的事情,并且官方網(wǎng)站也會(huì)做一些反爬蟲(chóng)措施,所以實(shí)現(xiàn)起來(lái)比較麻煩,最好的辦法就是使用現(xiàn)成的免費(fèi)接口,空氣質(zhì)量指數(shù)分析,這里是筆者自己實(shí)現(xiàn)的一個(gè)python調(diào)用方式
代碼如下:
# -*- coding: utf-8 -*- # flake8: noqa __author__ = 'wukong' import urllib from urllib import urlencode #配置您申請(qǐng)的appKey和openId app_key="***" open_id="***" """ request_url 請(qǐng)求地址 params 請(qǐng)求參數(shù) method 請(qǐng)求方法 """ def request_content(request_url,params,method): params = urlencode(params) if method and method.lower() =="get": f = urllib.urlopen("%s?%s" % (request_url, params)) else: f = urllib.urlopen(request_url, params) content = f.read() print content def main(): domain="http://api.xiaocongjisuan.com/"; servlet="life/air/analysis" method="get" request_url=domain+servlet #字典 params ={} params["appKey"]=app_key params["openId"]=open_id #變動(dòng)部分 params["city"]="成都" request_content(request_url,params,method) if __name__ == '__main__': main()
java版的代碼可以參考如下代碼:
package com.xiaocongjisuan.module.example; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; public class Application { public static final String DEF_CHATSET = "UTF-8"; public static final int DEF_CONN_TIMEOUT = 30000; public static final int DEF_READ_TIMEOUT = 30000; public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36"; //配置您申請(qǐng)的appKey和openId public static final String APP_KEY ="yours"; public static final String OPEN_ID ="yours"; //將map型轉(zhuǎn)為請(qǐng)求參數(shù)型 public static String urlEncode(Map<String,Object> params) { if(params==null){return "";}; StringBuilder sb = new StringBuilder(); for (Map.Entry<String,Object> i : params.entrySet()) { try { sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } String r=sb.toString(); if(r.endsWith("&")){ r = r.substring(0,r.length()-1); } return r; } /** * * @param requestUrl 請(qǐng)求地址 * @param params 請(qǐng)求參數(shù) * @param method 請(qǐng)求方法 * @return 請(qǐng)求結(jié)果 * @throws Exception */ public static String requestContent(String requestUrl, Map<String,Object> params,String method) throws Exception { HttpURLConnection conn = null; BufferedReader reader = null; String rs = null; try { //組裝請(qǐng)求鏈接 StringBuffer sb = new StringBuffer(); if(method!=null&&method.equalsIgnoreCase("get")){ requestUrl = requestUrl+"?"+urlEncode(params); } //默認(rèn)get URL url = new URL(requestUrl); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); if(method!=null&&method.equalsIgnoreCase("post")){ conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); } //參數(shù)配置 conn.setRequestProperty("User-agent", userAgent); conn.setUseCaches(false); conn.setConnectTimeout(DEF_CONN_TIMEOUT); conn.setReadTimeout(DEF_READ_TIMEOUT); conn.setInstanceFollowRedirects(false); conn.connect(); if (params!= null && method.equalsIgnoreCase("post")) { try { DataOutputStream out = new DataOutputStream(conn.getOutputStream()); out.writeBytes(urlEncode(params)); } catch (Exception e) { e.printStackTrace(); } } //讀取數(shù)據(jù) InputStream is = conn.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET)); String strRead = null; while ((strRead = reader.readLine()) != null) { sb.append(strRead); } rs = sb.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { reader.close(); } if (conn != null) { conn.disconnect(); } } return rs; } public static void main(String[] args) throws Exception{ String domain="http://api.xiaocongjisuan.com/"; String servlet="life/air/analysis"; String method="get"; String requestUrl=domain+servlet; Map<String,Object> params=new HashMap<String,Object>(); params.put("appKey",APP_KEY); params.put("openId",OPEN_ID); //變動(dòng)部分 params.put("city","成都"); String result=requestContent(requestUrl,params,method); System.out.println(result); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。