Java爬虫入门三

总体步骤: 

  1. 创建HttpClient对象

  2. 输入网址

  3. 发起请求

  4. 解析响应

  5. POST请求

上代码

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 爬虫post请求
 */
public class SpyderPostTest {
    public static void main(String[] args) throws Exception {
        // 创建HttpClient对象
        HttpClient httpClient = HttpClients.createDefault();
        // 设置参数
        HttpPost httpPost = new HttpPost("https://www.baidu.com/serach");
        // 创建参数
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("keys", "java"));
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "utf8");
        httpPost.setEntity(formEntity);
        // 发起请求
        HttpResponse response = null;

        try {
            response = httpClient.execute(httpPost);
            // 解析响应
            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/103553071
今日推荐