使用java代码批量删除新浪微博

首先开骂,新浪微博@#@!,不经我同意就转发各种微博,制造垃圾,还不提供微博批量删除功能,摆明了的流氓行为,可耻可恨,遭人唾弃!

SSLClient.java

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class SSLClient extends DefaultHttpClient {

    public SSLClient() throws Exception {
        super();
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[]{tm}, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = this.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
    }
}

WeiboTest.java

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.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class WeiboTest {

    public static final String url = "https://weibo.com/aj/mblog/del?ajwvr=6";
    public static final String charset = "utf-8";

    public static void main(String[] args) throws Exception {
        /**
         * sina_weibo.txt由微博页面F12获得格式是
         * <div node-type="feed_list">
         *     <div action-type="feed_list_item"></div>
         *     <div action-type="feed_list_item"></div>
         *     <div action-type="feed_list_item"></div>
         *     <div action-type="feed_list_item"></div>
         *     <div action-type="feed_list_item"></div>
         *     ...
         * </div>
         */
        File file = new File("C:\\Users\\Nihaorz\\Desktop\\sina_weibo.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        StringBuffer sb = new StringBuffer();
        String str;
        while ((str = br.readLine()) != null) {
            sb.append(str).append("\n");
        }
        System.out.println(sb);
        br.close();

        Document doc = Jsoup.parseBodyFragment(sb.toString());
        Element body = doc.body();
        Elements elements = body.select("div[action-type=‘feed_list_item‘]");
        Iterator<Element> it = elements.iterator();
        HttpPost httpPost = new HttpPost(url);
        addHeader(httpPost);
        while (it.hasNext()) {
            Element element = it.next();
            String mid = element.attr("mid");
            System.out.println(mid);
            execute(httpPost, mid);
        }
    }

    private static void execute(HttpPost httpPost, String mid) throws Exception {
        HttpClient httpClient = new SSLClient();
        //设置参数
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        Map<String, String> map = new HashMap<String, String>();
        map.put("mid", mid);
        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> elem = iterator.next();
            list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
        }
        if (list.size() > 0) {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
            httpPost.setEntity(entity);
        }
        HttpResponse resp = httpClient.execute(httpPost);
        System.out.println(resp);
    }

    private static void addHeader(HttpPost httpPost) {
        httpPost.addHeader("Accept", "*/*");
        httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
        httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
        httpPost.addHeader("Connection", "keep-alive");
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        /**
         * 设置自己的cookie
         */
        httpPost.addHeader("Cookie", "***");
        httpPost.addHeader("Host", "weibo.com");
        httpPost.addHeader("Origin", "https://weibo.com");
        httpPost.addHeader("Referer", "https://weibo.com/1864722372/profile?rank=1&is_all=1");
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36");
        httpPost.addHeader("X-Requested-With", "XMLHttpRequest");
    }

}

原文地址:https://www.cnblogs.com/nihaorz/p/8295658.html

时间: 2024-10-28 20:56:08

使用java代码批量删除新浪微博的相关文章

java移动文件夹、 慎用java file.renameTo(f)方法 、 java从一个目录复制文件到另一个目录下 、 java代码完成删除文件、文件夹 、

java移动文件夹(包含子文件和子文件夹): http://blog.csdn.net/yongh701/article/details/45070353 慎用java    file.renameTo(f)方法: http://www.cnblogs.com/mrwangblog/p/3934506.html 注意看结果,从C盘到E盘失败了,从C盘到D盘成功了.因为我的电脑C.D两个盘是NTFS格式的,而E盘是FAT32格式的.所以从C到E就是上面文章所说的"file systems"

Java 代码完成删除文件、文件夹操作

import java.io.File; /** * 删除文件和目录 * * @author chen * */ public class DeleteFileUtil { /** * 删除文件,可以是文件或文件夹 * * @param fileName * 要删除的文件名 * @return 删除成功返回true,否则返回false */ public static boolean delete(String fileName) { File file = new File(fileName)

springmvc+mybatis用多选框批量删除的功能Java代码

今天写了一个批量删除的功能,在后台传值过程中一直出错,最终还是请教了北京的一位高手帮我解决的,在此首先要好好感谢他,以后我有幸能帮助别人的话,决不推辞. 废话不说,直接进入正题,我会将在编写过程中出现的一些小知识点也进行总结概括的. 此项目基于的框架是:springmvc+mybatis (1)controller /**      * 批量删除 batch      */     @RequestMapping("/batchDeletes")     @ResponseBody  

使用ant批量将corba的idl文件生成java代码

这次有30多个idl文件需要生成java代码,并且其中有些idl之间相互引用,用命令一个个生成感觉非常反锁,并且容易出问题,看到jacorb中的例子是用ant来生成的,与自己也用ant批量生成了,在这里记录下主要的代码内容,供下次参考. ant的build.xml文件内容如下: <?xml version="1.0"?> <!-- 批量将idl生成java代码 --> <project name="idlBuilder" default

java对文件的创建 删除 批量创建 批量删除

package com.hc.fileio; import java.io.File; import java.io.IOException; /** * @author MaskedMen *对文件及文件夹的操作 */ public class OperatorFile { public static void main(String[] args) { deleteDirectorFile("D:\\XXX.txt"); try { createFileByName(false,&

java代码编辑器 pdf文件预览 主流SSM 代码生成器 shrio redis websocket即时通讯

A代码编辑器,在线模版编辑,仿开发工具编辑器,pdf在线预览,文件转换编码 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,快速开发利器)+快速表单构建器 freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类,service等完整模块 C 集成阿里巴巴数据库连接池druid  数据库连接池  阿里巴巴的 druid.Druid在监控.可扩展性.稳定性和性能方面都有明显的优势 D 集成安全权限框架shiro   Shiro 是一个用 Jav

使用easyui实现列表的批量删除

使用easyui实现列表的批量删除 首先要做的就是添加一个多选框 <table id="otGrid" nowrap="false" style="height: 330px;"> <thead> <tr> <th data-options="checkbox:true"></th> //就是这个,多选框 在列表的配置选项中 添加一个 singleSelect:fal

网上图书商城项目学习笔记-015删除和批量删除购物车条目

一.流程分析 二.代码 1.view层 (1)list.jsp 1 <a href="<c:url value='/CartItemServlet?method=batchDelete&cartItemIds=${item.cartItemId }'/>">删除</a> 2 3 <a href="javascript:batchDelete();">批量删除</a> 4 /* 5 * 批量删除 6

Hibernate批量更新和批量删除批量添加 1(转)

通常,在一个Session对象的缓存中只存放数量有限的持久化对象,等到Session对象处理事务完毕,还要关闭Session对象,从而及时释放Session的缓存占用的内存.批量处理数据是指在一个事务中处理大量数据.以下程序在一个事务中批量更新CUSTOMERS表中年龄大于零的所有记录的AGE字段:Transaction tx = session.beginTransaction();Iterator customers=session.createQuery("from Customer c