目录
我们用postman往csdn我们任意一篇文章发起post请求
只要你点进去
就会增长阅读量
结合我们学习的计算机网络相关知识
不难猜测是只要向网页发送post请求
就能获得一次阅读量的增长。
POST请求是HTTP协议中的一种方法,用于向服务器提交数据,通常用于创建或更新资源,或者执行一些操作,比如提交表单数据、上传文件等。在Web开发中,POST请求比GET请求更安全,因为它将数据包含在请求体中,而不是像GET请求一样在URL中可见。
打开postman
证实了我们的想法
我们只要往文章发送请求就行
利用网络编程的知识
我们写个发起请求的类
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.nio.charset.StandardCharsets;
- import java.util.Random;
-
- public class PostRequestSender {
- private String url;
- static long ans=0;
- public PostRequestSender(String url) {
- this.url = url;
- }
-
- public void sendPostRequest() throws IOException {
- // 创建URL对象
- URL obj = new URL(url);
-
- // 打开连接
- HttpURLConnection con = (HttpURLConnection) obj.openConnection();
-
- // 设置请求方法为POST
- con.setRequestMethod("POST");
-
- // 设置请求头
-
- con.setRequestProperty("User-Agent", "Java client");
- con.setRequestProperty("Content-Type", "application/json");
-
- // 可选:设置请求体
- Random random=new Random();
- int randomValue = random.nextInt(1000); // 生成一个0到999的随机整数
- String postData = "{\"key1\":\"value1\", \"key2\":\"" + randomValue + "\"}";
- con.setDoOutput(true);
- try (OutputStream os = con.getOutputStream()) {
- byte[] input = postData.getBytes(StandardCharsets.UTF_8);
- os.write(input, 0, input.length);
- }
-
- // 获取响应
- int responseCode = con.getResponseCode();
- System.out.println("Sending POST request to URL: " + url);
- System.out.println("Response Code: " + responseCode);
- System.out.println("run "+(ans++)+" ...");
-
- // 读取响应内容
- try (BufferedReader in = new BufferedReader(
- new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
- String inputLine;
- StringBuilder response = new StringBuilder();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- // System.out.println("Response Body: " + response.toString());
- }
- }
- }
在同一时间内连续发送多次post请求
只会记录一次阅读量
这说明
CSDN自带有反爬的程序
我们可以规定在1分钟内依次爬取100篇文章
利用的是多线程的知识
创建线程池
我们遍历数组
- import java.io.IOException;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
-
- public class Main {
-
- public static void main(String[] args) throws Exception {
- String[] urls = {
- "https://blog.csdn.net/qq_30500575/article/details/139987333?spm=1001.2014.3001.5501",
- "https://blog.csdn.net/qq_30500575/article/details/139962431?spm=1001.2014.3001.5501",
- "https://blog.csdn.net/qq_30500575/article/details/139962247?spm=1001.2014.3001.5501",
- "https://blog.csdn.net/qq_30500575/article/details/139926411?spm=1001.2014.3001.5501",
- "https://blog.csdn.net/qq_30500575/article/details/139920678?spm=1001.2014.3001.5501",
- "https://blog.csdn.net/qq_30500575/article/details/139920678?spm=1001.2014.3001.5501",
- "https://blog.csdn.net/qq_30500575/article/details/139904327?spm=1001.2014.3001.5501",
- "https://blog.csdn.net/qq_30500575/article/details/139904250?spm=1001.2014.3001.5501",
- "https://blog.csdn.net/qq_30500575/article/details/139841808?spm=1001.2014.3001.5501",
- "https://blog.csdn.net/qq_30500575/article/details/139841808?spm=1001.2014.3001.5501",
- "https://blog.csdn.net/qq_30500575/article/details/139842155?spm=1001.2014.3001.5501",
- "https://blog.csdn.net/qq_30500575/article/details/139857617?spm=1001.2014.3001.5501",
- "https://blog.csdn.net/qq_30500575/article/details/139857996?spm=1001.2014.3001.5501",
- "https://blog.csdn.net/qq_30500575/article/details/139858097?spm=1001.2014.3001.5501"
- // Add more URLs as needed
- };
-
- // 创建定时执行的线程池
- ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
-
- // 循环访问多个网址,并每隔20秒执行一次
- for (String url : urls) {
- Runnable task = () -> {
- try {
- PostRequestSender sender = new PostRequestSender(url);
- sender.sendPostRequest();
- } catch (IOException e) {
- e.printStackTrace();
- }
- };
- // 初始延迟10秒,每隔50秒执行一次任务
- executor.scheduleAtFixedRate(task, 10, 50, TimeUnit.SECONDS);
- }
-
- // 等待所有任务完成后关闭线程池
- executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
- executor.shutdown();
- }
- }
启动 查看控制台信息
注意联网哦
依次爬取打印
同时还会打印状态码和URL地址
https://blog.csdn.net/qq_30500575/category_12624592.html?spm=1001.2014.3001.5482
https://blog.csdn.net/qq_30500575/category_12642989.html?spm=1001.2014.3001.5482
https://blog.csdn.net/qq_30500575/category_12651993.html?spm=1001.2014.3001.5482
https://blog.csdn.net/qq_30500575/category_12699801.html?spm=1001.2014.3001.5482
https://blog.csdn.net/qq_30500575/category_12630954.html?spm=1001.2014.3001.5482
https://blog.csdn.net/qq_30500575/category_12701605.html?spm=1001.2014.3001.5482
https://blog.csdn.net/qq_30500575/category_12620276.html?spm=1001.2014.3001.5482