总体步骤:
-
创建HttpClient对象
-
输入网址
-
发起请求
-
解析响应
-
带参数的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();
}
}
}