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();
        }

    }
}
发布了59 篇原创文章 · 获赞 33 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_41010294/article/details/103553044