用Java获取网页数据(json)

之前的一个项目,当时甲方想要能够获取当地各个小区的平均房价,但是又不想加钱,用Java写了个简易的小玩意,贴上思路和代码。

首先浏览了各个二手房中介网站,最终选中了某二手房中介,因为这个网站不仅返回的数据是json,并且支持模糊查询,真是业界良心啊!

,有了url地址之后一切都变的简单起来,

使用jdk自带的HttpsURLConnection,把请求标头塞入进去之后,跑一下!

可以,简单又好用!

贴上完整代码

    @RequestMapping("/api/user/getAJK")
    public String getAJK() throws IOException {
        String kw = URLEncoder.encode("青阳新村","UTF-8");//编译注入数据
        String ur="https://hf.anjuke.com/esf-ajax/community/pc/autocomplete?city_id=33&kw="+kw+"&type=3";//拼接访问地址
        System.out.println(ur);
        URL url=new URL(ur);
        HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();//打开对象  下面是设置请求标头
        urlConnection.addRequestProperty("accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
        urlConnection.addRequestProperty("accept-encoding","gzip, deflate, br");
        urlConnection.addRequestProperty("accept-language","zh-CN,zh;q=0.9");
        urlConnection.addRequestProperty("cache-control","max-age=0");
        urlConnection.addRequestProperty("cookie","aQQ_ajkguid=221DB63B-4C16-D5A1-C869-D707E7506308; 58tj_uuid=0e22b29d-5947-4554-863d-983dd0b38a91; new_uv=1; als=0; id58=CrIW6mP4SShgbR6rLCBQAg==; _ga=GA1.2.1306140873.1677216043; _gid=GA1.2.1843287372.1677216043; ajk-appVersion=; ctid=33; fzq_h=82117c01fd2293f5bc5815c3d4c9f280_1677216046297_1e025f5215a945fd8c96e63ed64a541e_1740351643; seo_source_type=0; sessid=95932B2D-92BD-44E9-D5A8-9284C53D6E23; obtain_by=2; twe=2; xxzl_cid=6233dee1b0b64617a96b035578046167; xxzl_deviceid=QYLL8UXg9I9GglcnXjyzQGvoz8J+ssREpaw+j2ykhuIMzSV6XbjXiXNw3XD9fE+g");
        urlConnection.addRequestProperty("sec-ch-ua","\"Chromium\";v=\"110\", \"Not A(Brand\";v=\"24\", \"Google Chrome\";v=\"110\"");
        urlConnection.addRequestProperty("sec-ch-ua-mobile","?0");
        urlConnection.addRequestProperty("sec-ch-ua-platform","Windows");
        urlConnection.addRequestProperty("sec-fetch-dest","document");
        urlConnection.addRequestProperty("sec-fetch-mode","navigate");
        urlConnection.addRequestProperty("sec-fetch-site","none");
        urlConnection.addRequestProperty("sec-fetch-user","?1");
        urlConnection.addRequestProperty("upgrade-insecure-requests","1");
        urlConnection.addRequestProperty("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36");
        urlConnection.connect();//链接地址
        InputStream inputStream1 = urlConnection.getInputStream();//获取返回数据
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream1));
        String line;
        StringBuilder re= new StringBuilder();//获取到完整的json
        while ((line= bufferedReader.readLine())!=null){
            re.append(line);
        }
        return re.toString();
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值