java-使用HttpClient爬虫

大致思路

使用的工具为Httpclient
主要分为两种:1、不需要登录就能访问的网页 2、需要登录才可以访问的网页

1、不需要登录的网页
①构建浏览器
②确定访问的uri
③获取到响应并解析

2、需要登录才可以访问的网页
①构建浏览器
②获取cookie(一般情况下只需要cookie,有的时候还需要其他的请求头,例如authorization请求头)
第一种:静态获取:直接在浏览器中复制即可(有时候cookie没有用,换一个浏览器可能可以)
第二种:模拟程序获取:如果不知道登录的请求uri和参数,可以先在浏览器中输入错误的账号、密码,得到用于登录的请求uri以及需要提交的表单的参数;
然后在程序中想这个uri提交正确的表单,得到response,并得到Cookie
③按照浏览器中这个网址指定的请求方式,在程序中确定正式网页的请求uri和请求方式
④添加必要的几个请求头(例如Cookie、User-Agent等)
⑤获取到响应,并解析

完工!

简单的HttpClient使用模板

如果是采用模拟程序获取Cookie的话,通常是显示用Post提交表单,获取到Cookie,然后在通过Get方式携带Cookie,爬取到想要的网页信息

Get方式请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* 三步走:
* ①创建CloseableHttpClient对象,作为客户端
* ②创建HttpGet响应(先创建URIBuilder对象,再设置参数,参数设置结束后就创建HttpGet对象)
* ③获取CloseableResponse响应
* <p>
* ④开始解析响应
*/

public class HttpGetParamTest {
public static void main(String[] args) throws Exception {

// ① 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();

//设置请求的地址是:http://yun.itheima.com/search?key=java
// ②① 创建URIBuilder
URIBuilder uriBuilder = new URIBuilder("https://mooc1.chaoxing.com/exam/test/reVersionPaperMarkContentNew?courseId=222650590" +
"&classId=50617144&p=1&id=57211393&ut=s&cpi=95361626&newMooc=true&openc=cfd40dfe5645b94bd7f0740626b30614");
// ②② 设置参数
//uriBuilder.setParameter("courseId", "222650590");//可以添加多个setParameter()

// ③ 创建HttpGet对象,设置URL访问地址
HttpGet httpGet = new HttpGet(uriBuilder.build());
// ④ 如果是需要登录的网页的内容,则需要添加Cookie头的信息(注意:有时候edge对应的Cookie没有用,比如这一次的就是用的ie浏览器中展示出来的Cookie值)
httpGet.addHeader("Cookie", "两种方式获取到的Cookie");

httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100" +
".0.4896.127 Safari/537.36 Edg/100.0.1185.50");

// ⑤ 使用HttpClient发起请求,获取response
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
//解析响应
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(content);
System.out.println(WriteToFile.write(content));
// System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭Response
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
//关闭HttpClient
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Post方式请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 带参数的POST请求,四步走:
* ①创建CloseableHttpClient客户端
* ②创建HttpPost对象(声明一个List集合——>存放参数NameValuePair类型——>使用该List集合创建表单实体UrlEncodedFormEntity对象——>设置表单的Entity对象到Post
* 请求当中——>构建出HttpPost对象)
* ③获取CloseableHttpResponse响应
* ④解析响应,获取content(String类型)
*/

public class HttpPostParamTest {
public static void main(String[] args) throws UnsupportedEncodingException {
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();

//创建HttpPost对象,设置URL访问地址
HttpPost httpPost = new HttpPost("https://mooc1.chaoxing.com/exam/test/reVersionPaperMarkContentNew");
//声明List集合,封装表单中的参数
List<NameValuePair> params = new ArrayList<NameValuePair>(); //post发送构造函数只接受List<? extends
// NameValuePair>为参数,所以不能使用map
params.add(new BasicNameValuePair("courseId", "222650590"));
params.add(new BasicNameValuePair("cpi", "95361626"));
params.add(new BasicNameValuePair("newMooc", "true"));
params.add(new BasicNameValuePair("openc", "cfd40dfe5645b94bd7f0740626b30614"));
//创建表单的Entity对象,第一个参数是表单,第二个参数是编码
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "utf-8");
//设置表单的Entity的对象到Post请求中
httpPost.setEntity(formEntity);

//使用HttpClient发起请求,获取response
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);

//解析响应
if (response.getStatusLine().getStatusCode() == 200) { //状态码200表示请求成功
String content = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("========================响应数据长度:" + content.length());
}

} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭Response
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
//关闭httpClient
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Contents
  1. 1. 大致思路
  2. 2. 简单的HttpClient使用模板
    1. 2.1. Get方式请求
    2. 2.2. Post方式请求
|