search

Home  >  Q&A  >  body text

网页爬虫 - java如何模拟浏览器请求获取json文件

最近在想怎么不用jsonp来完成跨域的json获取并解析,大概的思路就是服务器端获取json,解析,客户端,只需要在本都调我的这个接口就行了,但是本人能力有限,怎么来模拟浏览器获取json文件呢

PHP中文网PHP中文网2813 days ago438

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-18 09:21:12

    I found a piece of code on the Internet and modified it. It should be able to meet your requirements (I used fastjson, I have never used it before, so I searched it now)

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.List;
    import java.util.Map;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    public class getJson {
        public static void main(String[] args) {
            //查询Ip信息的接口,返回json
            String url="https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=2.24.1.1&resource_id=6006&format=json";
            String result = "";
            BufferedReader in = null;
            try {
                String urlNameString = url;
                URL realUrl = new URL(urlNameString);
                // 打开和URL之间的连接
                URLConnection connection = realUrl.openConnection();
                // 建立实际的连接
                connection.connect();
                // 获取所有响应头字段
                Map<String, List<String>> map = connection.getHeaderFields();
                // 定义 BufferedReader输入流来读取URL的响应
                in = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            } catch (Exception e) {
                System.out.println("发送GET请求出现异常!" + e);
                e.printStackTrace();
            }
            // 使用finally块来关闭输入流
            finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            //得到的json数据
            System.out.println(result);
            //解析,
            JSONObject jsonObj = JSON.parseObject(result); 
            JSONArray jarr =  jsonObj.getJSONArray("data");
            JSONObject j0 = (JSONObject)jarr.get(0);
            //输出该ip对应的地理位置
            System.out.println(j0.get("location"));
        }
    
    }
    

    The process is probably like this. I personally think it is troublesome to parse json with a strongly typed language like java. If you don’t have to use java, it would be more appropriate to use python to do this

    reply
    0
  • Cancelreply