Java爬虫入门二

总体步骤: 

  1. 创建HttpClient对象

  2. 输入网址

  3. 发起请求

  4. 解析响应

  5. 带参数的GET请求

 上代码

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URISyntaxException;

/**
 * 带参数的get请求爬虫
 */
public class SpyderGetParamTest {
    public static void main(String[] args) throws URISyntaxException {
        // 创建HttpClient对象
        HttpClient httpClient = HttpClients.createDefault();
        // 输入网址
        String url = "https://www.baidu.com/serach";
        // 创建uri对象
        URIBuilder uriBuilder = new URIBuilder(url);
        // 设置参数
        uriBuilder.setParameter("keys","java");
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        // 发起请求
        HttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            // 解析响应
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                String content = EntityUtils.toString(entity, "utf8");
                System.out.println(content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

 

posted @   初见洞洞拐  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示